多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码。
我们需要通过子类重写父类的析构函数来释放,即父类的析构函数改为虚析构或者纯虚析构
虚析构和纯虚析构:
#include<iostream>
using namespace std;
#include<string>
//
//
//class animal{
//public:
// virtual void speak() = 0;
//};
//
//class Cat : public animal{
//public:
// Cat(string name){
// cout << "构造函数调用!" << endl;
// this->catName = new string(name);
// }
// void speak(){
// cout << *catName <<"小猫在说话!" << endl;
// }
// string * catName;
//};
//
//void what(animal * animal){
// animal->speak();
// delete animal;
//}
//
//int main(){
// what(new Cat("tom"));
// system("pause");
// return 0;
//}
class Animal {
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
virtual void Speak() = 0;
//析构函数加上virtual关键字,变成虚析构函数
virtual ~Animal()
{
cout << "Animal虚析构函数调用!" << endl;
}
//virtual ~Animal() = 0;
};
//Animal::~Animal()
//{
// cout << "Animal 纯虚析构函数调用!" << endl;
//}
//和包含普通纯虚函数的类一样,包含了纯虚析构函数的类也是一个抽象类。不能够被实例化。
class Cat : public Animal {
public:
Cat(string name)
{
cout << "Cat构造函数调用!" << endl;
m_Name = new string(name);
}
virtual void Speak()
{
cout << *m_Name << "小猫在说话!" << endl;
}
~Cat()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
public:
string *m_Name;
};
void test01()
{
Animal *animal = new Cat("Tom");
animal->Speak();
//通过父类指针去释放,会导致子类对象可能清理不干净,造成内存泄漏
//怎么解决?给基类增加一个虚析构函数
//虚析构函数就是用来解决通过父类指针释放子类对象
delete animal;
}
int main() {
test01();
system("pause");
return 0;
}