虚析构函数,派生类调用基类构造方法
#include <iostream> using namespace std; class A{ public: A() { cout<<"construct A"<<endl; } A(int a) { cout<<"construct int A"<<endl; } virtual ~A() //析构函数 必须 声明为 虚函数,才能彻底释放内存空间 { cout<<"destory A"<<endl; } }; class B : public A{ public: B() { cout<<"construct B"<<endl; } B(int a):A(a) //构造器调用A中int构造方法 { cout<<"construct int B"<<endl; } ~B() { cout<<"destory B"<<endl; } }; class C:public B{ public: C() { cout<<"construct C"<<endl; } ~C() { cout<<"destory C"<<endl; } }; int main() { A *p = new C(); delete p; return 0; }