Effective C + + Notes

Source: Internet
Author: User

Item 07: Declaring the virtual destructor for a polymorphic base class

1#include <iostream>2 using namespacestd;3 4 classBase {5  Public:6Base (): BValue (1) {}7     Virtual~base () {cout <<"Base destructor"<<Endl;}8     //~base () {cout << "Base destructor" << Endl;}9     Virtual voidprint () {Tencout <<"Print:base"<<Endl; One     } A Private: -     intBValue; - }; the  - classDerived: PublicBase { -  Public: -Derived (): Dvalue ( -) {} +~Derived () { -cout <<"Derived destructor"<<Endl; +     } A     voidprint () { atcout <<"print:derived"<<Endl; -     } - Private: -     intDvalue; - }; -  in intMain () { -base* p =NewDerived (); toP->print (); +     Deletep; -}
View Code

Note Line 7 and 8, when the destructor of the base class is Line7, that is, when the base class destructor is virtual, when the delete P is executed at LINE32, it is equivalent to invoking the destructor referred to by P, since P is a base-class pointer to the derived class object. And the destructor in the base class is virtual, so the occurrence condition of polymorphism is satisfied, that is, the destructor of the derived class is called Line19. Derived classes automatically call the destructor of their base class at the time of the destructor, so there is the result.  

If the destructor of the base class is no longer virtual (Line7 is commented out and replaced with Line8), so that when the delete P is called at LINE32, p itself is a base-class pointer, but the pointer is a derived class object. At this point if the function called by P is virtual, then p invokes the function of the same name directly in the derived class, which is described above, and if the function called by P is not virtual, it will call the function of the base class honestly.

So after replacing the destructor of the base class with non-virtual (that is, after commenting out Line7 for Line8), the result is as follows:

The result of this is that the derived class object referred to by P is freed only by the base class part, and the part of the derived class object that belongs to the derived class remains on the heap, causing a memory leak.

==========================================================================================================

1#include <iostream>2 using namespacestd;3 4 classPoint {5  Public:6Point (intXcoord,intYcoord);7~Point ();8 Private:9     intx, y;Ten }; One  A classPoint2 { -  Public: -Point2 (intXcoord,intYcoord); the     Virtual~Point2 (); - Private: -     intx, y; - }; +  - classPoint3 { +  Public: APoint3 (intXcoord,intYcoord): X (Xcoord), Y (Ycoord) {} at~Point3 () {} - Private: -     intx, y; - }; -  - intMain () { incout <<sizeof(point) <<Endl; -cout <<sizeof(Point2) <<Endl; topoint*p; +Point3 obj (1,2); -     return 0; the}
View Code

This code wants to express two questions:

1. Virtual functions take up some extra space due to VPTR,VTBL. The difference between point and Point2 is whether the destructor is virtual and the output is 8,12.

2. The purpose of my Point3 This class is to show that for Point and Point2, the constructors and destructors in the class are only declared without a definition (both directly and at the end). In the case of their member functions-without a definition, they are not prevented from being sizeof and defining pointers (L31), but they cannot define objects (like L32).

Effective C + + Notes

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.