When polymorphism occurs, it is best to set the destructor of the base class to virtual.

Source: Internet
Author: User

During polymorphism, it is best to set the destructor of the base class to virtual. In this way, the destructor of the subclass will be called first, and then the destructor of the base class will be called, otherwise, if Delete is a pointer to the base class, only the destructor of the base class will be called. example:

# Include <iostream. h>

Class base

{

Public:

Base () {mptr = new int ;}

Virtual ~ Base () {Delete mptr; cout <"~ Base: Base () "<Endl ;}

PRIVATE:

Int * mptr;

};

Class derived: public Base

{

Public:

Derived () {mderived = new long ;}

~ Derived () {Delete mderived; cout <"~ Derived: derived () "<Endl ;}

PRIVATE:

Long * mderived;

};

Void main ()

{

Base * P = new derived;

Delete P;

}

Result:

First, call the sub-class destructor: Release mderived and output "~ Derived: derived ()";

Call the destructor of the parent class: Release mptr and output "~ Base: Base ()".

------------

# Include <iostream. h>

Class base

{

Public:

Base () {mptr = new int ;}

~ Base () {Delete mptr; cout <"~ Base: Base () "<Endl ;}

PRIVATE:

Int * mptr;

};

Class derived: public Base

{

Public:

Derived () {mderived = new long ;}

~ Derived () {Delete mderived; cout <"~ Derived: derived () "<Endl ;}

PRIVATE:

Long * mderived;

};

Void main ()

{

Base * P = new derived;

Delete P;

}

Result: only the destructor of the parent class are called: Release mptr and output "~ Base: Base ()".

The above code will cause memory leakage, because the newly generated derived class resources are received using a base class pointer. During the analysis, the compiler only knows that the pointer is a base class. Therefore, it only analyzes the memory of the base class, instead of the Child class, causing memory leakage, if you change the destructor of the base class to a virtual function, you can avoid this situation, because the virtual function is bound after it is actually in the virtual function list, the Destructor replaces the destructor of the base class with a set of destructor of the actual object, that is, the virtual function of the Child class is executed before the virtual function of the parent class is executed, in this way, the sub-class memory analysis structure is completed, and the parent class memory is released, so that memory leakage will not occur.

In the above Code, such ~ Base () is not virtual, and ~ If derived () is virtual

Base * P = new derived; Delete P;

Still only call ~ Base: Base (), because P in Delete P is of the base * type, and the destructor of base ~ Base () is not virtual.

 

In addition, when the delete subclass pointer is used, the destructor of the subclass will be called, and then the destructor of the parent class will be automatically called (regardless ~ Derived: whether derived () is virtual ,~ Derived: derived () is automatically called internally ~ Base: Base (), for example:

Derived * Pd = new derived;

Delete PD; // call ~ Derived: derived (), and automatically analyzes the parent class (call ~ Base: Base ())

 

Note:

1. The Destructor is actually a function, regardless of its subclass or parent class, although the name may seem different. In addition, the Destructor are executed by sub-classes and then to the parent class.

2. When polymorphism occurs, you must write the Destructor as a virtual function to prevent memory leakage. each subclass maintains its own internal data release.

Virtual is the basis for implementing Polymorphism

It delays the jump of a specific function from compilation to runtime. However, the calling of the constructor is determined during the compiler, so it cannot be virtual.

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.