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
请按任意键继续. . .

posted on 2016-06-16 15:13  maxpak  阅读(197)  评论(0编辑  收藏  举报