C++ learning log - using virtual destructor and pure virtual destructor to solve memory leaks

Table of contents

1. Problem introduction

2. Use virtual destructor to solve

3. Use pure virtual destructor to solve

Four. Summary


1. Problem introduction

        When using polymorphism, if some subclass members are allocated in the heap area, there is no way to release the memory of the subclass after the parent class is released, which will cause memory leaks. The following code snippet.

        If there is no data in the heap area, you don't need to write virtual destructor or pure virtual destructor.

#include <iostream>
#include <string>
using namespace std;

class Animal{
  public:
    Animal(){
        cout<<"Animal-构造"<<endl;
    }
      ~Animal(){
        cout<<"Animal-析构"<<endl;
    }
    virtual void Run()=0;  //纯虚函数无需实现,只需声明
};

class Cat:public Animal{
    
  public:
  string *s_name;
  
  Cat(string name){
      s_name = new string(name);//在堆区创建内存
      cout<<"Cat-构造"<<endl;
  }
  
  void Run()
  {
      cout<<*s_name<<"->"<<"Cat-Run"<<endl;
  }
  ~Cat(){
      cout<<"Cat-析构"<<endl;
      if(s_name!=NULL){
          delete s_name;
          s_name=NULL;
      }
  }
};
int main()
{
    Animal *a;
    
    a = new Cat("Tom");
    a->Run();
    delete a;   //父类指针析构的时候,不会调用子类析构函数

   return 0;
}

 operation result:

       As a result, it can be seen that there are parent class and subclass structures. Although the parent class is deleted in the main function, there is only the destructor of the parent class in the end. At this time, the s_name created by the subclass in the heap area has not been released, resulting in memory leakage.

We introduce virtual destructor and pure virtual destructor to solve the above problems - the problem that the parent class pointer is not clean when releasing the subclass object

2. Use virtual destructor to solve

        The virtual destructor only needs to add the keyword virrtual before the destructor, and then observe the results, you can see that both the parent class and the subclass execute the destructor, and the data created in the heap area in the subclass is also released Clean, here is the final result!

   virtual ~Animal(){
        cout<<"Animal析构"<<endl;
    }

3. Use pure virtual destructor to solve

        The format of pure virtual destructor is as follows, which is somewhat similar to pure virtual function, but requires specific declaration and specific implementation. Pure virtual destructors need to be implemented outside the class.

class Animal{
  public:
    Animal(){
        cout<<"Animal-构造"<<endl;
    }
    
    //虚析构
     /*virtual ~Animal(){
        cout<<"Animal析构"<<endl;
    }*/
    //纯虚析构
    virtual ~Animal()=0;
    virtual void Run()=0;  //纯虚函数无需实现,只需声明
};

//需要有声明,也需要有实现
Animal::~Animal(){
    cout<<"纯虚析构"<<endl;
}

The result is as follows, which has the same effect as virtual destructor

 

Four. Summary

        virtual destructor and pure virtual destructor

                The same point:   both can solve the parent class pointer to release the subclass object, and both need specific implementation

                The difference:   pure virtual destructor is an abstract class, and objects cannot be instantiated

Guess you like

Origin blog.csdn.net/qq_53734051/article/details/126478376