virtual function of C + + destructor

Source: Internet
Author: User

Before the interview often asked about why the destructor is a virtual function in C + +, although this problem has been asked in the interview, but this problem is not a particularly good comprehensive solution on the Internet, recently looked at effective C + +, Clause 07 explained very good, Filter out part of the share to everyone.

When a derived class object is deleted through a base-class pointer, and the destructor of the base class is not virtual, the result is undefined: the actual execution usually occurs when the object's derived component is not destroyed, and the destructor of the derived class is not executed, the accumulated component is destroyed, and the memory leaks, resource leaks, corrupted data structures, etc. A virtual destructor is given to the base class, which can prevent the destruction of the base class but not the condition of the derived class, and use the pointer of the base class to manipulate the members of the inheriting class, releasing the resources of the inheriting class during the release of the pointer, and then calling the destructor of the base class.

If a class does not contain virtual functions, it is not good to make its destructor a virtual function, because this increases the memory overhead. To implement a virtual function, an object must carry some information that is used primarily to determine which virtual function is called at run time. This information is usually indicated by the vptr pointer, each object has a vptr pointer to an array of function pointers, called VTBL (virtual table), each class with a virtual function has a corresponding virtual table, when the object calls a virtual function, The function that is actually called depends on the virtual table that the virtual pointer of the object refers to: The compiler looks for the appropriate function pointer in it.

If a virtual function is undefined, it can increase the number of pointers and thus increase the memory overhead, which may affect the portability of functions written to other languages, so if a class is not used as a base class, or is not intended for polymorphism, it should not be declared as a virtual destructor.

virtual function of C + + destructor

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.