Methods derived from the ban class in C ++ and solutions to related problems

Source: Internet
Author: User
In my article "Role of virtual destructor in C ++", I explained why the Destructor used as the base class must be virtual functions, and also pointed out: to avoid the generation of virtual function tables, if the class is not a base class, the Destructor do not need to be declared as a virtual function.
However, we cannot predict the user's behavior. You are not sure whether the user will derive its own class from your class. If you use a base class pointer to delete an object of A derived class, the destructor of the derived class will not be called. I think everyone knows the danger of doing so. Of course, you can explain in the class description document, or even in the Class header file that the class you write cannot be used as the base class. But who can guarantee that the user will read these instructions carefully?
Therefore, our best method is to ban the derivation of classes. If the user derives his class from your class, he will know that this is wrong during the compilation phase, this avoids the risk that the Destructor that may occur in the running stage will not be called. The method derived from the ban class is to declare the constructor as private. For example, the following classes cannot be derived:

Class clxnotbase
{
Public:
~ Clxnotbase ();

PRIVATE:
Clxnotbase ();
Clxnotbase (const clxnotbase & RHs );
};

If the user derives a class from the class clxnotbase, he will get an error message that cannot access the private member function during the compilation phase.
Of course, you will certainly say: if we declare the class constructor as private, then we will not be able to construct the Class Object. What else can I use this class?
Yes, you are right. However, we can solve this problem in a simple way. The modified clxnotbase class is as follows:

Class clxnotbase
{
Public:
~ Clxnotbase ();

Static clxnotbase * newlxnotbase ();
Static clxnotbase * newlxnotbase (const clxnotbase & RHs );

PRIVATE:
Clxnotbase ();
Clxnotbase (const clxnotbase & RHs );
};

Clxnotbase * clxnotbase: newlxnotbase ()
{
// Call the real Constructor
Return new clxnotbase ();
}

Clxnotbase * clxnotbase: newlxnotbase (const clxnotbase & RHs)
{
// Call the real copy constructor
Return new clxnotbase (RHs );
}

When you want to use the clxnotbase class, you can call the pseudo constructor newlxnotbase to generate objects. Of course, each pseudo constructor calls New, which means that you must call Delete after using an object like clxnotbase. However, releasing unused resources is the basic quality of every C ++ programmer. Moreover, with the smart pointer auto_ptr, You can automatically delete the objects you are referring to. If you know how to use the smart pointer, it will be better. The following is an example:

Auto_ptr <clxnotbase> P (clxnotbase: newlxnotbase ());

In this case, you do not need to consider the delete object issue. When an object leaves the scope, the smart pointer automatically deletes the object.

// From: http://dev.csdn.net/author/starlee/258c5d2a0e1e45d5b0fcddc18d6db593.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.