C ++ virtual destructor

Source: Internet
Author: User

 

Another feature of the type that requires the virtual destructor is that the type has pointer members or reference members. If there are pointer and reference members, this type usually requires destructor and copy operations.

 

 

 

Generally, a type that implements destructor also needs to implement the copy constructor and copy Copy function.

 

 

 

As an empirical rule:

 

If you have a class with the virtual function, it needs a virtual destructor for 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 with new.

 

 

 

3. if a derived class object is assigned with new and is 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.

 

What do I still don't understand as a protective destructor? Here is an explanation: http://www.bkjia.com/kf/201110/410909.html. Understanding. I will try again later.

 

 

 

The following is an online explanation of the Destructor declared as protected:

 

1. If a class is inherited and a member object other than the base class is defined, 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 destructor as virtual can solve this problem, when other member functions are not virtual functions,

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 define the Destructor as protected (this will cause errors when the automatic object and delete are used outside the base class and the derived class, because the access permission is forbidden to call the destructor ),

This will not cause the above problems.

2. Ensure that the object is only generated on the stack.

I saw a blog post and asked a similar question. The excerpt is as follows:

In some programs, constructor and destructor are declared as private and protected. How can we create an object? The constructor cannot be called from the outside, but the object must be constructed. How can this problem be solved? For more information about constructor construction, how can I declare the constructor as private and protected, thank you! 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.

 

You must know the syntax.

So why are constructors or destructor declared as protected or private sometimes?

 

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, then you can declare the constructor/destructor of Class A as protected, and the constructor/destructor of the subclass of Class A as public. For example:

Class

{Protected: (){}

Public :....

};

Calss B: public

{Public: B (){}

....

};

 

A a; // error

B B; // OK

 

2. If you declare the constructor/destructor as private, only the "internal" function of this class can construct the object of this class. The "internal" mentioned here does not know whether you can understand it. The following is an example.

Class

{

Private:

A (){}

~ A (){}

 

Public:

Void Instance () // A function inside Class

{

A;

}

};

The above code 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 fail

AObj. Instance ();

However, if the Instance is a static function, it can be called directly without passing through an object. As follows: class

{

Private:

A (): data (10) {cout <"A" <endl ;}

~ A () {cout <"~ A "<endl ;}

 

Public:

Static A & Instance ()

{

Static A;

Return;

}

 

Void Print ()

{

Cout <data <endl;

}

 

Private:

Int data;

};

 

A & ra = A: Instance ();

Ra. Print ();

 

The above code is actually a simple C ++ code implementation in the singleton mode of the design mode.

 

 

Another case is that the copy constructor and operator = (the value assignment operator is overloaded) are declared as private, but no entity is implemented.

This is intended to prevent external users of a class from copying objects of this class.

For details, see a clause in Objective C ++. The specific terms are not remembered. Find it by yourself

 

Author: at the forefront of technology, I want to look down on everything.

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.