struct B { B(){ cout<<"B"<<endl;} ~B(){cout<<"~B"<<endl;} }; struct C { C(){ cout<<"C"<<endl;} ~C(){ cout<<"~C"<<endl;} }; struct D:B { D(){cout<<"D"<<endl;} ~D(){cout<<"~D"<<endl;} private: C c; };
D *d = new D; delete d;
运行结果: 可以看到,类D,构造函数,显示初始化化基类的构造函数,然后再分配成员变量C的内存,最后再执行自身构造函数~~