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.

Under normal circumstances, all the class destructor release the memory resources, and if the Destructor is not called, memory leakage will occur. I think all c ++ programmers know this danger. Of course, if you do other work in the destructor, all your efforts will be in vain.
Therefore, the answer to the question at the beginning of the article is -- This is to call the destructor of a derived class when a base class pointer is used to delete an object of A derived class.
Of course,Not all class destructor should be written as virtual functions. When there is a virtual function in the class, the compiler will add a virtual function table to the class to store the virtual function pointer, which will increase the storage space of the class.. Therefore, only when a class is used as the base class can the Destructor be written as a virtual function.

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 that the base class destructor is virtual.
Virtual void display () {cout <"Hello, world! Cbase \ n ";} // virtual function

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 are 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; // stack object
Cbase * pbase = new cderived; // The base class Pointer Points to the object of the derived class. This is a heap object.

Cout <"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 that, if the destructor of the base class is not virtual, only the cbase object can be parsed here, which is of the same type as the pointer.
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 derived class destructor 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.