测试了一下c++的析构函数:
#include <iostream>
#include<boost/thread.hpp>
#include<fstream>
#include<memory>
using namespace std;
using namespace boost::posix_time;
time_duration td = seconds(4); //01:00:01
class A{
public:
A(){
}
~A(){
cout << "on ~A()" << endl;
}
};
class Test{
public:
Test(){
ptr = make_shared<A>();
}
~Test(){
cout << "on ~Test()" << endl;
ofstream output("dddd.log");//
output << "ddddfaf" << endl;
boost::this_thread::sleep(td);//模拟某个耗时的操作
cout << "~test() returned" << endl;
}
private:
std::shared_ptr<A> ptr;
};
int main()
{
Test t;
}
输出:
on ~Test()
~test() returned
on ~A()
结果发现:先执行析构函数,再调用成员的析构函数