1、无虚析构函数
#include <iostream>
using namespace std;
class CParent{
public:
CParent(){}
~CParent(){
cout<<"调用基类的析构函数"<<endl;
}
};
class CChild:public CParent
{
public:
CChild(){}
~CChild(){
cout<<"调用派生类的析构函数"<<endl;
}
};
int main()
{
CParent *pc;
pc = new CChild();
delete pc;
}
2、有虚析构函数
#include <iostream>
using namespace std;
class CParent{
public:
CParent(){}
virtual ~CParent(){
cout<<"调用基类的析构函数"<<endl;
}
};
class CChild:public CParent
{
public:
CChild(){}
~CChild(){
cout<<"调用派生类的析构函数"<<endl;
}
};
int main()
{
CParent *pc;
pc = new CChild();
delete pc;
}