Why is the requirement for destructor in C ++ virtual?

Source: Internet
Author: User

This is because the basic class pointer is used to destroy the behavior of the derived class object. If the base class does not have a virtual destructor, a problem occurs. We know that there is no problem in deleting the pointer object. The destructor of the pointer object will be called correctly, but only the size of the object indicated by the pointer type. If a base class Pointer Points to its derived class, deleting this base class pointer can only delete the part of the base class object, but cannot delete the entire derived class object, the reason is that the destructor of the derived class cannot be accessed through the base class pointer.
However, if, like other virtual functions, the destructor of the base class are also virtual, The destructor of the derived class must also be virtual. When deleting the base class pointer, it will find the correct derived class destructor through the virtual function table and call it to correctly analyze the entire derived class object.

This is like generating a dynamic array and releasing its space. Is the space of the entire array rather than one element to be released. For example:

Char * itsstring = new char [2];
Delete [] itsstring;

If [] is added, the entire array is deleted. If it is not added, only the first element of itsstring is deleted.
Delete: delete refers to releasing the memory space occupied.

For example, if the base class pointer is equivalent to itsstring, the entire class is like [] itsstring. What we really want to analyze is the entire object rather than a part of the object.

Let's look at the Code:

# Include <iostream> using namespace STD; Class cbase {public: cbase () {cout <"cbase constructor... \ n";} virtual ~ Cbase () {cout <"cbase Destructor... \ n ";}// specifies the virtual void display () {cout <" Hello, world! Cbase \ n ";}// void Hello () {cout <" Call cbase hello. \ n ";}// non-virtual function }; class cderived: Public cbase {public: cderived () {cout <" cderived constructor... \ n ";}~ Cderived () {cout <"cderived Destructor... \ n ";}// this is also a virtual destructor, because the destructor in the base class is a virtual void display () {cout <" Hello, world! Cderived \ n ";}// virtual function, which overwrites the virtual function in the base class. Void Hello () {cout <"Call cderived hello. \ n ";}// non-virtual function }; void main () {cderived derived; // The cbase * pbase = new cderived object in the stack; // point the base class pointer to the object of the derived class. Here is the cout object in the heap <"derived. hello (); \ n \ t "; derived. hello (); cout <"pbase-> Hello (); \ n \ t"; // Because hello () is a non-virtual function, you can only access methods in the base class. Pbase-> Hello (); cout <"derived. display (); \ n \ t "; derived. display (); cout <"pbase-> display (); \ n \ t"; // The method in the derived class is called correctly due to the relationship between virtual functions. Pbase-> display (); cout <"delete pbase; \ n \ t";/* the problem is here. If the destructor of the base class is not virtual, in this case, only cbase objects can be parsed, which is of the same type as pointers. After the base class uses the virtual destructor, the base class pointer can (through the virtual function table) access the virtual destructor of the derived class and call the destructor of the derived class to structure the derived class itself, you can see that the entire derived class has been destructed in turn. */Delete pbase; // The base class is a virtual destructor, And the destructor of the derived class is also virtual. The entire derived class is correctly destructed. Cout <"---------------- \ n ";}

Next, I would like to raise another concern:

Why cannot constructors be virtual?

The reason is that the object does not exist when constructing itself. The virtual function requires a virtual function table, but this table does not exist in the construction phase and at least no memory is allocated. Therefore, definition requirements cannot be met.

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.