C ++ virtual destructor and virtual functions combined with question variants

Source: Internet
Author: User

Combination of virtual destructor and virtual functions with question variants
1. [negative tive c ++ principle 07]: Declares virtual destructor for the polymorphism base class.
[If not]: if it is not declared as a destructor, the following results may occur: the components of the Derived object are not destroyed, causing resource leakage and a long time waste in debugging.
Class CSimpleClass
{
Public:
CSimpleClass () {cout <"CSimpleClass" <endl ;}
~ CSimpleClass () {cout <"~ CSimpleClass "<endl ;}
Private:
};

Class CDerived: public CSimpleClass
{
Public:
CDerived () {cout <"CDerived" <endl ;}
~ CDerived () {cout <"~ CDerived "<endl ;}
Private:
};

Int main ()
{
CSimpleClass * pSimple = new CDerived;
Delete pSimple;

Return 0;
}


 
The execution result is as follows:



 

 

Apparently, the CDerived object is not destructed!
1. What are the reasons for these differences?
The "C ++ standard" explicitly states that when the derived class object is deleted by a base class pointer pBaseObject, the base class carries a non-virtual destructor, the results are not defined (unpredictable ). In actual execution, as shown in the first figure above, a bug occurs, and the object of the derived class is not destroyed.
This forms a strange "partial destruction" object, forming a resource leak.

2. When do I need to declare the basic class destructor as a virtual function? When does the destructor of the base class not need to be a virtual function?
This problem involves when the Destructor should be a virtual function. NOTE: For the above base class BaseClass,
If the Destructor is not a virtual function, sizeof (BaseClass) = 1.
If the Destructor is a virtual function, sizeof (BaseClass) = 4.
The size of classes containing constructors and non-virtual destructor is 1 byte. Explanation:
The size of the empty class, such as BaseClass, is not constructor or destructor. Originally, sizeof (BaseClass) should be 0, but we declare that this type of instance must occupy a certain amount of space in the memory, otherwise, these instances cannot be used. The compiler determines how much memory is occupied. Each null instance in visual studio occupies 1 byte of space.
What about constructor, destructor, or other non-virtual functions? Because the addresses of these non-virtual functions are only related to the class, but not to the instance of the class, the compiler will not add any additional information because of the increase of non-virtual functions.
So why is the size of the Destructor four bytes after it becomes a virtual function? The main reason is: Once C ++ finds that there are virtual functions in the class, it will generate a virtual function table for the class and add a pointer to the virtual function table in each instance of this type. On a 32-bit machine, a pointer occupies 4 bytes, so the sizeof size is 4. On 64-bit machines, a pointer occupies 8 bytes of space, so the size of sizeof is 8.
It is declared as a virtual destructor at the cost of memory. Therefore, if you declare the destructor of all classes as virtual functions without any reason, it is wrong to declare them as virtual functions. Follow the prosperous game http://www.shengshiyouxi.com

Summary:
(1) declare a virtual destructor for a base-class application with polymorphism. If the class contains any virtual function, it should have a virtual destructor;
(2) virtual destructor should not be declared if the purpose of the design class is not used as a base class or for Polymorphism purposes.
-- Refer to Objective C ++ clause 7 and offoffoffer

 
2. [valid principle 09]: virtual functions are never called during constructor and destructor.
[Cause]: the execution of the base class is earlier than that of the derived class constructor. When the base class constructor is executed, the member variables of the derived class have not been initialized.
[If not]: The execution result will not be dynamically associated, and the virtual function at the layer where it is located will still be executed.
[Example ]:
Class CSimpleClass
{
Public:
CSimpleClass () {cout <"CSimpleClass" <endl; foo () ;}// calls the foo
Virtual ~ CSimpleClass () {cout <"~ CSimpleClass "<endl; foo () ;}// calls the foo
Virtual void foo () {cout <"CSimpleClass: foo ()" <endl ;}
Private:
};

ClassCDerived: public CSimpleClass
{
Public:
CDerived () {cout <"CDerived" <endl; foo ();}
~ CDerived () {cout <"~ CDerived "<endl; foo ();}
Void foo () {cout <"CDerived: foo ()" <endl ;}
Private:
};

Int main ()
{
CSimpleClass * pSimple = new CDerived;
Delete pSimple;

Return 0;
}


 
The execution result is as follows:

 



 

 
3. The comprehensive questions of 1 and 2 are as follows:
Class CBase
{
Public:
CBase () {cout <"CBase ctor" <endl; foo () ;}// call the foo
~ CBase () {cout <"CBase dtor" <endl; foo () ;}// virtual is not added AND foo of the current layer is called.
Private:
Virtualvoid foo () {cout <"Base: foo ()" <endl ;}//
};

Class CDerived: public CBase
{
Public:
CDerived () {cout <"CDerived ctor" <endl; foo ();}
~ CDerived () {cout <"CDerived dtor" <endl; foo ();}

Private:
Virtual void foo () {cout <"Derived: foo ()" <endl ;}
};

Int main ()
{
CBase * pBase = new CDerived;
Delete pBase;

Return 0;
}

 

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.