(c + +) on the declaration of a polymorphic base class destructor as a virtual function

Source: Internet
Author: User

Main content:

1. Constructors and destructors in C + + class inheritance

2. Static binding and dynamic binding in C + + polymorphism

3. The destructor is declared as virtual function in C + + polymorphism

1. Constructors and destructors in C + + class inheritance

In the class inheritance of C + +,

When an object is created, the constructor of the base class is called first, and then the constructor of the next derived class is called, and so on;

When the object is reconstructed, its order is exactly the opposite of the construction;

Specific reference article: http://www.cnblogs.com/AndyJee/p/4575385.html

2. Static binding and dynamic binding in C + + polymorphism

The static type of the object: the type of object in which the declaration is made, determined at compile time;

The dynamic type of the object: the type that the current object refers to, and at run time, the dynamic type of the object can be changed, but the static type cannot be changed.

Static binding: The static type of the object is bound, and an attribute (such as a function) depends on the static type of the object, which occurs at compile time.
Dynamic binding: Binding is the dynamic type of an object, and an attribute (such as a function) depends on the dynamic type of the object, which occurs at run time.

Specific reference article: http://www.cnblogs.com/AndyJee/p/4575670.html

3. The base class destructor is declared as virtual function in C + + polymorphism

Let's take a look at some examples of programs:

    • Declaring a base class destructor as a virtual function
#include <iostream>using namespace Std;class person{public:    virtual ~person () {  //declare destructor as a virtual function    cout << "Person::~person ()" << Endl;    };  Class Student:public Person{public:    ~student () {     //virtual or not is OK        cout << "student::~student ()" << Endl;    }}; int main () {person    *pt1 = new person;    Person *pt2 = new Student;        Base class pointer point-to-derived class    //Student *PT3 = new person;     Derived class pointer can not point to base class    Student *pt4 = new Student;    Delete pt1;    cout << "*********" << Endl;    Delete pt2;    cout << "*********" << Endl;    Delete Pt3;    cout << "*********" << Endl;    Delete Pt4;    cout << "*********" << Endl;    return 0;}

Operation Result:

    • Do not declare a base class destructor as a virtual function:
#include <iostream>using namespace Std;class person{public:    ~person () {  //declare destructor as a virtual function    cout << "Person::~person ()" << Endl;    };  Class Student:public Person{public:    ~student () {     //virtual or not is OK        cout << "student::~student ()" << Endl;    }}; int main () {person    *pt1 = new person;    Person *pt2 = new Student;        Base class pointer point-to-derived class    //Student *PT3 = new person;     Derived class pointer can not point to base class    Student *pt4 = new Student;    Delete pt1;    cout << "*********" << Endl;    Delete pt2;    cout << "*********" << Endl;    Delete Pt3;    cout << "*********" << Endl;    Delete Pt4;    cout << "*********" << Endl;    return 0;}

Operation Result:

Can be seen:

when you point to a derived class with a base-class pointer ,

When the base class destructor is declared as virtual, the Delete base class pointer invokes the destructor of the derived class before calling the destructor of the base class.

When a base class destructor is not declared as virtual, the Delete base class pointer calls only the destructor of the base class, not the destructor of the derived class, which results in incomplete destruction of the object.

Analysis:

Person *pt2 = new Student;

The static type of PT2 is person, and the dynamic type is student,

When a destructor is a virtual function, for dynamic binding, delete Pt2 calls the destructor of the dynamic type, the derived class, and the destructor of the base class is called due to the inheritance relationship;

When a destructor is a non-virtual function, the static binding, delete pt2, calls the destructor of the static type, the base class, without invoking the destructor of the derived class.

(The above is purely personal understanding)

Summarize:
    • The virtual destructor should be declared for the polymorphic base class. Once a class contains virtual functions, it should contain a virtual destructor, because polymorphism, there must be a base class calling derived classes.

    • A virtual destructor should not be declared for a class if it is not used as a base class or if it does not need to be polymorphic.

Reference article:

Http://www.cnblogs.com/children/archive/2012/08/13/2636956.html

(c + +) on the declaration of a polymorphic base class destructor as a virtual function

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.