class A{ public: A(){ cout << "this is A" << endl; } ~A(){ cout << "delete A" << endl; } }; class B{ public: B(){ cout << "this is B" << endl; } ~B(){ cout << "delete B" << endl; } }; class C :public B, A { public: ~C(){ cout << "delete C" << endl; } C(){ cout << "this is class C" << endl; } }; int main() { C temp; return 0; }
运行结果为:
this is B
this is A
this is class C
delete C
delete A
delete B
1、C调用B,A的顺序是 由继承中声明的顺序决定的。
public B ,A; // 决定了先构造B,然后A
2、先调用父类的构造函数,然后再调用子类的构造函数
3、析构时先析构子类,再析构父类