C++ 派生类的析构函数的执行顺序
C++中派生类是不会继承父类的构造函数和析构函数的,这一点要明确。派生类中构造函数的执行顺序是先父类的构造函数然后当前类,析构函数则相反。举例如下:
#include <iostream> using namespace std; class A { public: A() { cout << "A constructor" << endl; }; ~A() { cout << "A destructor" << endl; }; }; class B : public A { public: B() { cout << "B constructor" << endl; }; ~B() { cout << "B destructor" << endl; }; }; class C : public B { public: C() { cout << "C constructor" << endl; }; ~C() { cout << "C destructor" << endl; }; }; int main() { C c; return 0; }
上述代码执行后效果如下: