#include <iostream>
using namespace std;
class Base
{
private:
int* a;
public:
Base(){
cout<<"base construct new int"<<endl;
a = new int();
}
//必须申明为虚析构函数,不然子类的析构函数将不会被执行
virtual ~Base(){
cout<<"base destruct del int"<<endl;
delete a;
}
};
class Child : public Base{
private:
int* b;
public:
Child(){
cout<<"Child construct new int"<<endl;
b = new int();
}
~Child(){
cout<<"Child destruct del int"<<endl;
delete b;
}
};
int main()
{
Base* b;
b = new Child();
delete b;
}
转载于:https://blog.51cto.com/fangnux/1388808