Why the destructor of a base class is defined as a virtual function __ function

Source: Internet
Author: User

Premise:
1: Each destructor clears only its own members (the member function has no virtual).
2: A pointer to a derived class may be the object of the base class, and when the destructor of a base class pointer to a member of a derived class is not known, the program may cause memory leakage, so the destructor of the base class is defined as a virtual function;
A base class pointer can point to an object (polymorphic) of a derived class. If you delete the pointer delete[]p, the destructor of the derived class that the pointer is directed to, and the object of the derived class automatically tune the member function of the base class, thus releasing the object of the derived class, if the destructor of the base class is not defined as a virtual function, The static binding that is implemented by the compiler, when a pointer to a base class is dropped, only the destructor of the base class is freed without releasing the member functions of the derived class, causing the problem of memory leaks due to the incomplete release of memory.
one: derived class pointers point to objects of derived classes

Class Base
{public
:
    base ()
    {
        cout << "base ()" << Endl;
    }
    Virtual ~base ()
    {
        cout << "~base ()" << Endl;
    }

;
Class Driver:p ublic Base
{public
:
    Driver ()
    {
        cout << "Driver ()" << Endl;
    }
    ~driver ()
    {
        cout << "~driver ()" << Endl;
    }

;
void Test ()
{
    driver*pd = new Driver;
    Delete pd;
}


The derived pointer points to the object of the derived class, releasing the "P" by releasing the destructor of the derived class and then releasing the destructor of the base class
Second: The pointer to the base class points to the object of the derived class (no virtual)

Class Base
{public
:
    base ()
    {
        cout << "base ()" << Endl;
    }
     ~base ()
    {
        cout << "~base ()" << Endl;
    }

;
Class Driver:p ublic Base
{public
:
    Driver ()
    {
        cout << "Driver ()" << Endl;
    }
    ~driver ()
    {
        cout << "~driver ()" << Endl;
    }

;
void Test ()
{
   Base *pd = new Driver;
    Delete pd;
}


When a pointer to a base class points to a member of an object of a derived class, the process of releasing PD is to release only the resources of the base class without invoking the destructor of the derived class; Such deletes can delete only the base class object, not the derived class object, causing memory leaks;
In public inheritance, the operation of the base class on the object of a derived class can only affect which inherited members, and if you want the base class to operate on a non inheritance, define the base class function as a virtual function;
Third: The pointer to the base class is directed to a derived class (with virtual)

Class Base
{public
:
    base ()
    {
        cout << "base ()" << Endl;
    }
    Virtual ~base ()
    {
        cout << "~base ()" << Endl;
    }

;
Class Driver:p ublic Base
{public
:
    Driver ()
    {
        cout << "Driver ()" << Endl;
    }
    ~driver ()
    {
        cout << "~driver ()" << Endl;
    }

;
void Test ()
{
   Base *pd = new Driver;
    Delete pd;
}


When a destructor of a base class is defined as a virtual function, the compiler does not implement a static binding so that when PD is released, the destructor of the derived class is released and the destructor of the base class is released.

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.