Private constructor in C ++

Source: Internet
Author: User

 

Generally, constructors are not private or protected members, but they can make private member functions. In some special cases, constructors are defined as private or protected members.

C ++ class constructors should not be private functions, because private functions cannot be used by other classes or global functions. To create a C ++ instance, you must call the constructor.

Therefore, if the constructor is a private function, other classes except their own methods cannot construct instances of this class. Generally, a class is used by other classes. To use a class, you must first construct its instance. Therefore, it is very difficult for other classes to use a constructor if it is a private function.

However, in some special cases, for example, if you do not want other classes to be able To instance Chinese classes, constructors are defined as private or protected.

The most common one is that a class has only one instance. To avoid other classes from creating multiple instances, the constructor is defined as private:

Class
{
PRIVATE:
Static A m_ins;
Public:
Static A & getinstance ();
PRIVATE:
A ();
};

A A: m_ins;

A & A: getinstance ()
{
Return m_ins;
}

....

In another case, you can define a friend, so that only a friend can create instances of this class, and other classes cannot.

Keyboard & keyboard: getinstance (){
Static keyboard; // unique keyboard object
Return keyboard; // return the reference of this object
}

The entire program has only one keyboard object. The getinstance function is used to obtain this unique object. The above code is a possible implementation method.

Why should we declare getinstance as static?
If it is not declared as static, an external keyboard object must be used to call its getinstance function. However, if the getinstance function is not called, the object cannot be obtained externally. In this way, a knot is formed.
If it is declared as static, The getinstance function can be called directly based on the class name (rather than the class object.

If the getinstance function is not static, it must be called as follows:
Keyboard KBD;
KBD. getinstance ();

If the getinstance function is static, you can call it as follows:
Keyboard: getinstance ();

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.