//多重继承 #include <iostream> using namespace std; class A { public: A() { cout<<"A基类构造A::A()"<<endl; } ~A() { cout<<"A基类析构A::~A()"<<endl; } }; class B:public A { public: B() { cout<<"B子类构造B:B()"<<endl; } ~B() { cout<<"B子类析构B:~B()"<<endl; } }; class C:public A { public: C() { cout<<"C子类构造C::C()"<<endl; } ~C() { cout<<"C子类析构C::~C()"<<endl; } }; class D:public B,public C { public: D() { cout<<"D孙子类构造D::D()"<<endl; } ~D() { cout<<"D孙子类析构D::~D()"<<endl; } }; int main(int argc, char *argv[]) { D d; return 0; }
则可以看出构造顺序A->B->A->C-D;析构顺序相反。
时间: 2024-10-10 01:31:04