C++析构顺序
知识点:先构造的后析构,后构造的先析构
class Test { public: Test() { } ~Test() { cout<<"Test destructor"<<endl; } }; class Test1 { public: Test1() { } ~Test1() { cout<<"Test1 destructor"<<endl; } }; class Test2 { public: Test2() { } ~Test2() { cout<<"Test2 destructor"<<endl; } private: Test a; Test1 b; }; int main() { Test2 *c=new Test2; delete c; system("pause"); return 0; }
输出为
Test2 destructor
Test1 destructor
Test destructor
请按任意键继续. . .