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.