关于为什么要将析构函数定义成虚函数,为了加深印象,写了个小测试代码对比了一下:
#include <iostream>
using namespace std;
class Base{
public:
Base()
{
cout << "Constructor of Base" <<endl;
}
virtual ~Base() //注意添加virtual关键字的区别virtual
{
cout << "Destructor of Base" <<endl;
}
};
class Derived:public Base{
public:
Derived()
{
cout << "Constructor of Derived class" << endl;
}
~Derived()
{
cout << "Destructor of Devived classs" <<endl;
}
};
int main()
{
Derived obj; //test1
//Base* pBase = new Derived; //test2
//delete pBase; //tes