C + + Learning 24 Virtual destructor

Source: Internet
Author: User

In C + +, constructors are used to initialize objects when they are created and cannot be declared as virtual functions. Because the object has not yet been created before executing the constructor, the virtual function table does not already exist and there is no pointer to the virtual function table, so you cannot query the virtual function table at this time, and you do not know which constructor to call. The following section explains the concept of virtual function tables.

Destructors are used to complete the corresponding resource release work when destroying an object, which can be declared as a virtual function.

To illustrate the need for virtual destructors, let's look at one of the following examples:

#include <iostream>using namespacestd;//base classclassbase{Private:    int*A; Public: Base (); ~base () {cout<<"Base destructor"<<Endl;}}; Base::base () {a=New int[ -]; cout<<"Base Constructor"<<Endl;}//Derived ClassesclassDerived: Publicbase{Private:    int*b; Public: Derived (); ~derived () {cout<<"Derived destructor"<<Endl;}};D Erived::D erived () {b=New int[ -]; cout<<"Derived Constructor"<<Endl;}intMain () {Base*p =NewDerived; Deletep; return 0;}

In this example, two classes are defined, base class base and derived class Derived, both of which have their own constructors and destructors. In the constructor, a memory space of 100 int is allocated, and in the destructor, the memory is freed.

In the main function, the pointer p for the base class type is defined, pointing to the derived class object, and then you want to use Delete to release the space that P points to

As you can see from the running result, only the destructor of the delete p; base class is called when the statement is executed, but the destructor of the derived class is not called. This causes B to point to the 100 int type of memory space that is not released, and there is no chance of releasing the memory until the end of the program is reclaimed by the operating system. This is a typical memory leak.

Memory leaks are a problem that programmers need to avoid. The memory leak in this example is due to the fact that the destructor of the derived class is not called, and in order to solve this problem, the destructor of the base class needs to be declared as a virtual function. The revised code looks like this:

 class   base{ private  :  int  *A;  public     : Base ();  virtual  ~base () {cout<< " Span style= "color: #800000;" >base destructor   <<ENDL;}; Base::base () {A  = new  int  [100  ]; cout  << base constructor  Span style= "color: #800000;" > " <<endl;}  

Thus, the destructor of the derived class will automatically become a virtual destructor. When a delete p; statement is executed, the destructor of the derived class is executed before the destructor of the base class, so there is no memory leak problem.

This example is sufficient to illustrate the necessity of virtual destructors, but it is not appropriate to declare all of the base class's destructors as virtual functions, regardless of 3,721. In general, if there is a member variable in the base class that points to the dynamic allocation of memory, and the code that frees the dynamically allocated memory is defined in the destructor of the base class, then the destructor for the base class should be declared as a virtual function.

C + + Learning 24 Virtual destructor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.