Analysis of the use of C + + virtual destructor _c language

Source: Internet
Author: User
In C + +, You cannot declare a fictional function, but you can declare a virtual destructor. Polymorphism means that different objects have different behavioral characteristics to the same message. Virtual functions are the basis of run-time polymorphism, primarily for objects, and constructors run before objects are generated, so There's no point in making a fictional function .Of
The function of the destructor is to perform some necessary cleanup before the object is extinct, and the destructor is best to be virtual.
First, explain how the fictitious function and the pointer interact, and the concrete meaning of the virtual destructor. For example, the following code, where SomeClass is a class that contains a non-virtual destructor:
SomeClass *p= new SomeClass;
. . .  . . .
Delect p;
When you call Delect for P, the destructor of the SomeClass class is automatically invoked. Now, let's look at what happens after you mark the destructor as virtual.
Describe the interaction between destructor and virtual function mechanism, the simplest expression is that all destructors are considered to have the same name (even if they are not really the same names). For example, suppose the derived class is a derived class of the base class and assumes that the destructor in the base class is marked as virtual. Now let's analyze the following code:
Base *pbase= New Derived;
. . .  . . .
Delect pbase;
When you call Delect for base, a destructor is called. Destructors in the derived class are called because the destructor in the base class is marked as virtual and the object that is pointing to is of type derived.
It should be noted that after the destructor is marked as virtual, all destructors of the derived class are automatically virtual (regardless of the time they are marked with virtual). Similarly, this behavior is as if all destructors have the same name (even in fact different names).
Here's what it says about all the the benefit of the destructor being marked as virtual。 Suppose the base class has a member variable of a pointer type Pb,base the constructor of the class creates a dynamic variable that is pointed by PB, and the base class's destructor deletes the dynamic variable that the PB points to. Also, assuming that the base class is not marked as virtual and assumes that the derived class (which derives from base) has a member variable of a pointer type pd,derived the constructor of the class creates a dynamic variable with PD pointing to it. The destructor of the derived class deletes the dynamic variable that the PD points to. Analyze the following code:
Base *pbase= New Derived;
. . .  . . .
Delect pbase;
Because destructors in the base class are not marked as virtual, the destructor of the base class is only invoked. It returns the memory that the PB points to the dynamic variable to free storage. However, for a dynamic variable that PD points to, the memory it occupies will never be returned to free storage (unless the program terminates).
On the other hand, if the destructor of base class base is marked as virtual, then the destructor of the derived class is invoked when the Delect is invoked for PBase (because the object being pointed to is of type derived). The destructor of the derived class deletes the dynamic variable that the PD points to, and then automatically calls the destructor of the base class base. The latter removes the dynamic variable that the PB points to. Therefore, after the base class destructor is marked as virtual, all memory is successfully reclaimed by free storage. In order to prepare for this situation, it is best to always mark the destructor as virtual.
For example, explain:
Copy Code code 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 base has no virtual adornments, the output is:
constructor in Base.
Constructor in Derived.
Destroctor in Base.
It's like this. Destructors in class derived do not execute, causing memory leaks, so if a class is a base class for other classes, its destructor should be declared as a virtual destructor. In addition, we can see in this example the order in which constructors and destructors are executed. constructors, first base class, subclass, Destructor, first subclass, back 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.