Clause 07 declares virtual destructor for the polymorphism base class, And the polymorphism virtual

Source: Internet
Author: User

Clause 07 declares virtual destructor for the polymorphism base class, And the polymorphism virtual
Declare virtual destructor for the polymorphism base class

In the inheritance for Polymorphism purposes, if the virtual destructor is not declared for the base class, it will cause problems. refer to the following code.

class A {public:A() {cout << "A()" << endl; }~A() {cout << "~A()" << endl;}//virtual ~A() {cout << "~A()" << endl;}virtual void a() {cout << "function in A" << endl;}};class B:public A {public:B() { cout << "B()" << endl;}~B() {cout << "~B()" << endl;}};int main() {A *pa = new B;delete pa;return 0;}

The running result is:

The problem occurs: The base class of base class A points to the object of the derived class B, and the object is deleted by A base class pointer. At this time, the base class contains non-virtual destructor, this will cause a disaster. Because C ++ points out that when a derived class object is deleted by a base class pointer and the base class has non-virtual destructor, the result is undefined-in actual execution, the derived component of the object is not destroyed, but the base class is destroyed, as shown in the code, the destructor of A is called, but the destructor of the derived class B is not called. So this is a strange"Partial destructionThis will cause memory leakage, corrupt the data structure, and waste time debugging.

Solution: Add a virtual destructor to the base class. In this way, the entire object is destroyed by destroying the derived class object through the base class pointer. The execution result is shown in the virtual destructor shown in the annotation of the destructor of the Base Class.

Summary(1)Almost all classes with virtual functions should define a virtual destructor.. One experience is that virtual destructor are declared for a class only when the class contains at least one virtual function.

(2)If a class does not contain virtual functions, it usually indicates that it does not intend to act as the base class. When the class does not intend to act as the base class, it is an idea to make its destructor virtual.. Because of the existence of virtual functions, the class must have pointers to the virtual function table. In this way, the volume of class objects will increase (refer to sizeof computing class size ).


If the category is not a base class or not for polymorphism, do not declare a virtual destructor.

The preceding Declaration of a virtual destructor for the base class only applies to the polymorphism base class. This type is designed to process derived objects through a base class interface. HoweverNot all base classes are designed for the purpose of polymorphism.. For example, the string and STL containers in the table are not designed as the base class, so do not mention polymorphism. Therefore, they do not need virtual destructor.

If you try to inherit a class without any virtual destructor, including STL containers such as vector, list, And unordered_map, errors are easily caused.

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.