Protection and private constructors and destructors of C + +

Source: Internet
Author: User

First, the constructor function

1. Protection

When a constructor is defined as protected, it means that you cannot construct objects outside of the class, but only the objects of subclasses of that class, such as:

Class Base

{

Protected

Base () {}

...

};

Class Derived:public Base

{

Public

Derived () {}

...

};

Base b; Error

Derived D; Ok

2. Private

After a constructor is defined as private, it means not only that the object cannot be constructed outside of the class, but also that the object of the subclass of the class cannot be constructed externally, but only through the static static function of the class to access the internally defined object of the class. A single-piece singleton mode is a typical example of a private constructor:

Class CLog

{

Private

CLog () {};

Public

~clog () {};

Public

Static clog* getinstance ()

{

if (NULL = = m_soploginstance)

{

CLock Oinstancelock;

Oinstancelock.lock ();

if (NULL = = m_soploginstance)

{

M_soploginstance = new CLog ();

}

Oinstancelock.unlock ();

}

return m_soploginstance;

}

...

Private

Static CLog *m_soploginstance;

...

};

CLog &log = Clog::getinstance ();

3. Copy Construction and assignment operators

After the copy construction and assignment operators are defined as private, it means that copying operations on the objects of the class are prohibited externally. The typical application of this case is that when a member of a class has a lock member variable, the prohibit copy construction and assignment operation prevents the object from being copied, the copied object is locked and the original object is locked, thus violating the code Farmer's will.


Second, the destructor function

The objects in the heap are usually created/destroyed with New/delete, and when new is called, it automatically invokes the constructor of the corresponding class, which automatically invokes the destructor of the corresponding class when the delete is called. When the object is generated in the stack, the creation/destruction of the object is done automatically, that is, the constructor is called automatically at creation time, and the destructor is called automatically when it is destroyed, that is, it is not necessary to display the call New/delete, but there is a precondition that the constructor/destructor of the class must be public.

destructors, whether protected or PRIAVTE, are both forbidden to produce objects in the stack, because the invocation of the destructor cannot be done automatically, and the object cannot be created in the stack; Of course, if you create an object on the heap, you cannot delete the object directly. This also causes the object to be refactored externally, but the destruction of the heap object can be done indirectly, for example:

Class Base

{

Public

Base () {}

Private

~base () {}

Public

void Destroy () {delete this;}

};

Create a public destroy function for a class that has a private or protection destructor to destroy the object.

What is the difference between a private and a protection destructor? The difference is that the private destructor not only prohibits the generation of objects in the stack, but also prohibits inheritance, and if you do not want to be too ruthless to limit inheritance, then use the destructor of protection.


Protection and private constructors and destructors of C + +

Protection and private constructors and destructors of C + +

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.