Home>

The focus of this article:virtual destructors should be declared for polymorphic base classes.Once a class contains virtual functions,It should contain a virtual destructor.If a class is not used as a base class or does not need to be polymorphic,You should not declare a virtual destructor for it.

1. Reason:

When implementing polymorphism, When using a base class pointer to operate a derived class, When destructing, prevent only destructing base classes without destructing derived classes.

2. Examples:

(1),

Include #include<iostream>
Using namespace std;
Class base {
Public:
  (Base () {};
Base ~ base () {cout<"output from the destructor of class base!"<<endl;};
Void dosomething () {cout<""do something in class base!"<<endl;};
};
Class derived:public base {
Public:
  (Derived () {};
Der ~ derived () {cout<"output from the destructor of class derived!"<<endl;};
Do void dosomething () {cout<""do something in class derived!"<<endl;};
};
 Int main () {
 Derived * p=new derived;
P->dosomething ();
Delete p;
Return 0;
}

operation result:

Do something in class derived!

Output from the destructor of class derived!

Output from the destructor of class base!

The destructor of the base class in the code is not a virtual function,In the main function, use the pointer of the inherited class to operate the members of the inherited class.The process of releasing the pointer p is:first release the resources of the inherited class,Then release the base class resources.

(2),

#Include<iostream>
Using namespace std;
Class base {
Public:
  (Base () {};
Base ~ base () {cout<"output from the destructor of class base!"<<endl;};
Void dosomething () {cout<""do something in class base!"<<endl;};
};
Class derived:public base {
Public:
  (Derived () {};
Der ~ derived () {cout<"output from the destructor of class derived!"<<endl;};
Do void dosomething () {cout<""do something in class derived!"<<endl;};
};
 Int main () {
 Base * p=new derived;
P->dosomething ();
Delete p;
Return 0;
}

operation result:

Do something in class clxbase!

Output from the destructor of class clxbase!

The destructor of the base class in the code is also not a virtual function.The difference is that in the main function, the pointer of the base class is used to operate the members of the inherited class.The process of releasing the pointer p is:only release the resources of the base class,Instead of calling the destructor of the inherited class. Calling the dosomething () function also executes the function defined by the base class.

In general,Such deletion can only delete base class objects,You cannot delete subclass objects,Formed a delete half image,Cause a memory leak.

In public inheritance,Base class operations on derived classes and their objects,Only those members inherited from the base class are affected.If i want to operate on non-inherited members with a base class,This function of the base class should be defined as a virtual function. The destructor should also be the same:if it wants to redefine or new members and objects in a subclass,Of course it should also be declared false.

(3),

Include #include<iostream>
Using namespace std;
Class base {
Public:
  (Base () {};
~ Virtual ~ base () {cout<"output from the destructor of class base!"<<endl;};
Void virtual void dosomething () {cout<""do something in class base!"<<endl;};
};
Class derived:public base {
Public:
  (Derived () {};
Der ~ derived () {cout<"output from the destructor of class derived!"<<endl;};
Do void dosomething () {cout<""do something in class derived!"<<endl;};
};
 Int main () {
 Base * p=new derived;
P->dosomething ();
Delete p;
Return 0;
}

operation 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 the code is defined as a virtual function,In the main function, use the pointer of the base class to operate the members of the inherited class.The process of releasing the pointer p is:releasing the resources of the inherited class,Then call the destructor of the base class.Calling the dosomething () function also executes the function defined by the inherited class.

3. Summary:

The base class pointer can point to the object of the derived class (polymorphism). If you delete the pointer delete p;the derived class destructor pointed to by the pointer will be calledThe destructor of the derived class automatically calls the destructor of the base class,In this way, the object of the entire derived class is completely released.If the destructor is not declared as a virtual function,The compiler implements static binding,When removing the base class pointer,Will only call the destructor of the base class and not the destructor of the derived class,This will cause incomplete destruction of the derived class object.Therefore, it is necessary to declare the destructor as a virtual function.

c
  • Previous Text and image timing switching effect code implemented by JS
  • Next Android power information view (electricity, temperature, voltage) example code