為什么 void operator () (std::ofstream* fp) 被呼叫了?
#include <string>
#include <fstream>
#include <memory>
#include <cstdio>
#include <iostream>
using namespace std;
class FileDeleter
{
private:
std::string filename;
public:
FileDeleter(const std::string& fn) :filename(fn) {
std::cout << "FileDeleter()" << std::endl;
}
void operator () (std::ofstream* fp)
{
fp->close();
std::remove(filename.c_str());
std::cout << "operator ()" << std::endl;
}
};
int main()
{
std::shared_ptr<std::ofstream> fp(new std::ofstream("tmpfile.txt"), FileDeleter("tmpfile.txt"));
}
uj5u.com熱心網友回復:
因為share_ptr 的建構式為template< class Y, class Deleter >
shared_ptr( Y* ptr, Deleter d );
形式是會自動在析構的時候呼叫 d(ptr)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/175383.html
標籤:新手樂園
