虚析构函数
namespace space3
{/*析构函数 和 虚析构函数在父类指针指向子类对象时, ~Animal()函数前,如果没加virtual,子类的析构函数~Cat()不会被调用*/class Animal{public:Animal(){}virtual ~Animal(){cout << "~Animal " << endl;}virtual void Talk() { cout << "animal talk" << endl; }private:};class Cat:public Animal{public:Cat(const char* name){mp_name = new char[strlen(name) + 1];strcpy(mp_name, name);}~Cat(){cout << "~cat" << endl;if(mp_name)delete[] mp_name;}void Talk() { cout << "cat talk" << endl; }private:char* mp_name;};
}int main()
{using namespace space3;Animal* cat = new Cat("Tom");delete cat;return 0;
}