Home > Back-end >  From automatically calls the function problem
From automatically calls the function problem

Time:10-17

Why is void operator () (fp) STD: : ofstream * is called?

# include & lt; string>
#include
#include
#include
#include

using namespace std;


The class FileDeleter
{
Private:
STD: : string filename;

Public:
FileDeleter (const STD: : string& {fn) : filename (fn)

STD: : cout & lt; <"FileDeleter ()" & lt; }

Void operator () (STD: : ofstream * fp)
{

Fp - & gt; Close ();
STD: : remove (filename. C_str ());

STD: : cout & lt; }
};

Int main ()
{
STD: : from & lt; STD: : ofstream> Fp (new STD: : ofstream (" tmpfile. TXT "), FileDeleter (" tmpfile. TXT "));

}

CodePudding user response:

Because share_ptr constructor for
 templateFrom (Y * PTR, Deleter d); 

Form is automatically when the destructor call d (PTR)
  • Related