Analysis on the use of C ++ virtual destructor

Source: Internet
Author: User

In C ++, virtual constructor cannot be declared, but virtual destructor can be declared. Polymorphism means that different objects have different behavior characteristics for the same message. As the basis of Runtime polymorphism, virtual functions are mainly targeted at objects. constructor runs before an object is generated. Therefore, virtual constructor has no significance.
The function of destructor is to clear some necessary work before such objects die out. It is recommended that all destructor are virtual.
First, explain how the fictitious functions interact with pointers and the specific meaning of the virtual destructor. For example, in the following code, SomeClass is a class containing non-virtual destructor:
SomeClass * p = new SomeClass;
......
Delect p;
When delect is called for p, the destructor of the SomeClass class is automatically called. Now let's take a look at what will happen after you mark the Destructor as virtual.
The simplest way to describe the interaction between a destructor and a virtual function is to think of all the Destructor as having the same name (even if they are not really the same name ). For example, assume that the Derived class is a Derived class of the Base class and that the destructor in the Base class is marked as virtual. Analyze the following code:
Base * pBase = new Derived;
......
Delect pBase;
When delect is called for Base, a destructor is called. Because the destructor in the Base class are marked as virtual and the object to be directed belongs to the Derived type, the destructor in the Derived class is called.
It should be noted that after the Destructor is marked as virtual, all the destructor of the derived class are automatically virtual (no matter whether they are marked as virtual ). Similarly, this behavior is like all the Destructor have the same name (even though they actually have different names ).
The following describes the benefits of marking all destructor as virtual. Assuming that the Base class has a member variable pB of the pointer type, the Base class constructor will create a dynamic variable directed by pB, the destructor of the Base class will delete the dynamic variables pointed to by pB. In addition, assume that the Base class is not marked as virtual, and assume that the Derived class (Derived from Base) has a member variable pD of the pointer type, the constructor of the Derived class creates a dynamic variable pointed to by pD, and the destructor of the Derived class deletes the dynamic variable pointed to by pD. Analyze the following code:
Base * pBase = new Derived;
......
Delect pBase;
Because the destructor in the Base class are not marked as virtual, only the destructor of the Base class will be called. It will return the memory occupied by dynamic variables directed by pB to free storage. However, for a dynamic variable pointed to by pD, the memory occupied by it will never be returned to free storage (unless the program is terminated ).
On the other hand, if the destructor of the Base class Base is marked as virtual, then when pBase calls delect, it will call the destructor of the Derived class (because the object to which it points belongs to the Derived type ). The destructor of the Derived class deletes the dynamic variables pointed to by the pD, and then automatically calls the destructor of the Base class. The latter deletes the dynamic variables pointed to by pB. Therefore, after the basic class destructor is marked as virtual, all memory can be successfully recycled from free storage. To prepare for this situation, it is best to always mark the Destructor as virtual.
For example: Copy codeThe Code is as follows: # include "stdafx. h"
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base () {cout <"Constructor in Base." <endl ;}
Virtual ~ Base () {cout <"Destructor in Base." <endl ;}
};
Class Derived: public Base
{
Public:
Derived () {cout <"Constructor in Derived." <endl ;}
~ Derived () {cout <"Destructor in Derived." <endl ;}
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
Base * p = new Derived;
Delete p;
Return 0;
}

Output:
Constructor in Base.
Constructor in Derived.
Destroctor in Derived.
Destroctor in Base.

If the destructor in the Base is not virtual, the output is:
Constructor in Base.
Constructor in Derived.
Destroctor in Base.
In this way, the destructor in the subclass Derived is not executed, causing memory leakage. Therefore, if a class is the base class of other classes, declare the Destructor as a virtual destructor.In addition, the execution sequence of constructor and destructor can also be seen in this example. Constructor, first base class, then subclass, destructor, first subclass, then 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.