shared pointer

#include <string>
#include <fstream>
#include <memory>
#include <cstdio>

class FileDeleter
{
  private:
    std::string filename;
  public:
    FileDeleter(const std::string& fn)
    :filename(fn){}

    void operator()(std::ofstream* fp){
      fp->close();
      std::remove(filename.c_str());
    }
};

int main()
{
  std::shared_ptr<std::ofstream> fp(new std::ofstream("tmpfile.txt"),FileDeleter("tmpfile.txt"));
}

 

posted @ 2019-05-31 14:18  西北逍遥  阅读(364)  评论(0编辑  收藏  举报