Why is the destructor of the base class a virtual function in C++?

 
 

Note: Most of the content in this article is compiled from the website.

1. Why is the destructor of the base class a virtual function?

  When implementing polymorphism, when the base class is used to operate the derived class, the situation in which only the base class is destructed without destructing the derived class is prevented when destructing.

  The following is transferred from the network: source address http://blog.sina.com.cn/s/blog_7c773cc50100y9hz.html

  a. The first piece of code



#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl;};

    void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
    ClxDerived() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};
  int main(){  
  ClxDerived *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  }

operation result:

  Do something in class ClxDerived!            

  Output from the destructor of class ClxDerived!

  Output from the destructor of class ClxBase!  

  The destructor of the base class in this code is not a virtual function. In the main function, the pointer of the inherited class is used to operate the members of the inherited class. The process of releasing the pointer P is: first release the resources of the inherited class, and then release the resources of the base class. 

  b. The second piece of code




#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl;};

    void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
    ClxDerived() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; }
};
  int main(){  
  ClxBase *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  }

 Output result:

  Do something in class ClxBase!
  Output from the destructor of class ClxBase!

    The destructor of the base class in this code is also not a virtual function. The difference is that the pointer of the base class is used in the main function to operate the members of the inherited class. The process of releasing the pointer P is: only the resources of the base class are released, and The destructor of the inherited class is not called. Calling the dosomething() function executes the function defined by the base class.

    Under normal circumstances, such deletion can only delete the base class object, but cannot delete the subclass object, forming a deleted half image, resulting in a memory leak.

    In public inheritance, the operation of the base class on the derived class and its objects can only affect those members inherited from the base class. If you want to use the base class to operate on non-inherited members, you need to use this function of the base class. Defined as a virtual function.

    A destructor should naturally do the same: if it wants to destruct redefines or new members and objects in a subclass, it should of course also be declared virtual. 

 c. The third piece of code:
#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    virtual ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl;};
    virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
    ClxDerived() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};

  int main(){  
  ClxBase *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  }

operation result:

  Do something in class ClxDerived!
  Output from the destructor of class ClxDerived!
  Output from the destructor of class ClxBase!

    In this code, the destructor of the base class is defined as a virtual function. In the main function, the pointer of the base class is used to operate the members of the inherited class. The process of releasing the pointer P is: just release the resources of the inherited class, and then call the base class. The destructor of the class. Calling the dosomething() function executes the function defined by the inherited class.  

    If you do not need the base class to operate on derived classes and objects, you cannot define virtual functions, because this will increase memory overhead. When a virtual function is defined in a class, the compiler will add a virtual function table to the class to store it. Virtual function pointer, which will increase the storage space of the class. Therefore, only when a class is used as a base class, write the destructor as a virtual function.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992743&siteId=291194637