C ++ destructor & Virtual destructor

Source: Internet
Author: User

1. Why do I need to define a virtual destructor?

If oneClass with the virtual function, it needs a virtual destructorFor the following reasons:
1) if a class has a virtual function, it is often used as a base class;
2) if it is a base class, Its Derived classes are often allocated using new;
3) if a derived class object is allocated using new and controlled by a pointer to its base class, it often deletes it by a pointer to its base class. (If the base class does not have a virtual destructor, the result will be uncertain. when it occurs, the destructor of the derived class will never be called );
If a base class has a virtual destructor, The destructor of the underlying derived class is called first, and the destructor of each base class is called.

2. declared as a protected destructor

If a member object other than the base class is defined in a derived class and the base class destructor are not virtual, when the base class pointer or reference points to the object of the derived class and analyzes the structure (for example, when the automatic object ends at the end of the function scope; or through delete, it calls the destructor of the base class, so that the Members defined in the derived class are not destructed and Memory leakage occurs.
Although defining the Destructor as virtual can solve this problemWhen other member functions are not virtual functions (that is, no virtual functions exist in the base class)Vtable is introduced to the base class and derived class, And vptr is introduced to the instance, resulting in performance loss during running. If you do not need to use the base class directly but only through the derived class object,
You can setThe Destructor is defined as protected.(This will cause errors when automatic objects and delete are used outside the base class and derived class, because access permission is forbidden to call the destructor), this will not cause the above problems.

3. usage when constructor and destructor are declared as private and protected

In terms of syntax, a function is declared as protected or private, so this function cannot be called directly from the "external.
For protected functions, other functions of the subclass can be called;
Private functions can only be called by other functions in this class;

The common scenarios are as follows:
1) if you do not want external users to directly construct an object of A Class (assuming the class name is a), but want users to only construct a subclass of Class A, then you willYou can declare the constructor/destructor of Class A as protected, and the constructor/destructor of the subclass of Class A as public.

For example:

 
ClassA {Protected: (){}Public:...}; Calss B:PublicA {Public: B () {}......};;//ErrorB;//OK

2)If you declare the constructor/destructor as private, only the "internal" function of this class can construct the object of this class.
For example:

ClassA {Private: (){}~A (){}Public:VoidInstance ()//An internal function of Class.{;}};

The aboveCodeIt can be compiled. The instance function in the code above is an internal function of Class. An object of A is constructed in the instance function body.
However, this instance function cannot be called outside. Why?
To call the instance function, an object must be constructed. However, the constructor is declared as private. An object cannot be constructed externally.

 
A aobj;//Compilation and translation failAobj. instance ();

However, if the instance isStatic FunctionsThen,It can be called directly without passing through an object.

 Class  A {  Private  : A (): Data (  10 ) {Cout < "  A  " < Endl ;} ~ A () {cout < "  ~ A  " < Endl ;} Public  :  Static A & Instance (){  Static  A;  Return  A ;}  Void  Print () {cout <Data < Endl ;}  Private  :  Int  Data;}; & Amp; RA = A: instance (); RA. Print (); 

The above code is actually a design patternSingleton ModeA simple C ++ code implementation.

 

Original article: http://www.cnblogs.com/kanego/archive/2011/10/03/virtual_destructor.html

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.