C + +: When do you need to define a destructor as a virtual function? __jquery

Source: Internet
Author: User
C + + Exchange Group: 302558294 (Welcome to join)
Original Address: http://blog.csdn.net/jiadebin890724/article/details/7951461#comments, thank the original author to share
first, be clear:
1. Each destructor (without virtual) is responsible for clearing only its own members.
2. There may be a base class pointer pointing to a case that is indeed a member of a derived class. (This is quite normal),

Then when destructors point to a base class pointer to a member of a derived class, the program does not know what to do.

Therefore, to ensure that the appropriate destructor is run, the destructor in the base class must be a virtual destructor.

A base class pointer can point to an object (polymorphism) of a derived class. If you delete the pointer delete []p; it calls the derived class destructor that the pointer points to, and the destructor of the derived class automatically calls the base class's destructor, so that the entire derived class object is completely freed. If the destructor is not declared as a virtual function, the compiler enforces a static binding, and when the base class pointer is deleted, only the base class's destructor is called and the derived class destructor is not invoked, which causes the derived class object to be not completely destructor. Therefore, it is necessary to declare a destructor as a virtual function.


Sample code:

1. First paragraph 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;
}


Run 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, the member of the inheriting class is manipulated with the pointer of the inheriting class in the main function, and the process of releasing the pointer P is to release the resource of the inheriting class and then release the base class resource.


2. Second paragraph 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 Results: 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, but instead of manipulating the members of the inheriting class with a pointer to the base class in the main function, the process of releasing the pointer P is:

Simply frees the resource of the base class without invoking the destructor of the inheriting class. The call to the DoSomething () function executes a function that is also defined by the base class.
In general, such a deletion can only delete the base class object, but not the subclass object, resulting in the deletion of half image, resulting in memory leaks.
In public inheritance, the operations of a base class on a derived class and its objects can affect only those members inherited from the base class. If you want to manipulate a non-inherited member with a base class, define this function of the base class as a virtual function.
The destructor should naturally be the same: if it wants to deconstruct the redefinition or new members and objects in subclasses, it should be declared virtual as well.

3. Third paragraph 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;
}


Run 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 defined as a virtual function, in which the member of the inheriting class is manipulated with a pointer to the base class, and the process of releasing the pointer P is

: Only the resources of the inherited class are freed, and then the destructor of the base class is called. Call the DoSomething () function to perform a function that is also defined by the inheriting class.

If you do not need a base class to manipulate derived classes and objects, you cannot define virtual functions because this increases the memory overhead. When a class has a virtual function defined, the compiler adds a virtual function table to the class that holds the virtual function pointer,

This increases the storage space of the class. Therefore, the destructor is written as a virtual function only when a class is used as a base class.

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.