继承类的构造析构顺序

构造: 现有父类后有子类

析构:和构造顺序相反,先析构子类后析构父类

#include <iostream>

class A
{
public:
    A()
    {
        std::cout << "ctor father" << std::endl;
    }
    virtual ~A()
    {
        std::cout << "dtor fathor" << std::endl;
    }
};

class a : public A
{
public:
    a()
    {
        std::cout << "ctor son" << std::endl;
    }
    ~a()
    {
        std::cout << "dtor son" << std::endl;
    }
};

int main(void)
{
    a aa;
    return 0;
}

 

posted @ 2023-01-16 15:33  Wangtn  阅读(35)  评论(0编辑  收藏  举报