# 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 fortemplateFrom (Y * PTR, Deleter d);
Form is automatically when the destructor call d (PTR)