纯虚析构
#include <iostream>
class Person
{
public:
Person()
{
std::cout << "Person" << std::endl;
}
virtual ~Person() = 0;
public:
};
Person::~Person()
{
std::cout << "~Person" << std::endl;
}
class Student: public Person
{
public:
Student()
{
std::cout << "Student" << std::endl;
age = new(int);
}
~Student()
{
std::cout << "~Student" << std::endl;
delete age;
}
public:
int *age;
};
int main()
{
Person *p = new Student;
delete p;
return 0;
}
- 虚析构和纯虚析构是用来解决,父类指针释放子类对象
- 如果子类没有堆区数据,可以不写虚析构
- 拥有纯虚析构的类,也属于抽象类