纯虚析构

#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;
}
  1. 虚析构和纯虚析构是用来解决,父类指针释放子类对象
  2. 如果子类没有堆区数据,可以不写虚析构
  3. 拥有纯虚析构的类,也属于抽象类
posted @ 2022-07-11 15:46  thomas_blog  阅读(103)  评论(0编辑  收藏  举报