Design Mode-single-key mode (Singleton) C ++ implementation [reprinted]

Source: Internet
Author: User

In single-key mode, only one instance is allowed for one class!
The implementation method is generally as follows:
1. privatize all constructors and prohibit arbitrary external object construction;
2. Implement the unique object of the class and allow the user of the class to access the object in some way.
Following this idea, I thought of the simplest implementation, that is, defining a public static data member of the class for external use.
Class Singleton
{
PRIVATE:
// All Constructors
Public:
Static Singleton ms_instance;
....
};
But if you think about it, there is a problem with this method. The construction time of non-local static objects in C ++ cannot be determined. If this object is not constructed yet, a problem occurs when it is used; in addition, the static data member may have been initialized before the main function, but some data used by the object may have to wait Program It is valid only in the main function, so that the object construction may fail. Therefore, this method cannot be used.
Since static member objects cannot be used directly, you can use pointers to generate unique objects of this class when using them. Code :
Class Singleton
{
// Private all Constructors
Public:
Singleton & getinstance (void)
{
If (ms_pinstance = NULL)
Ms_pinstance = new Singleton ();
Return * ms_pinstance;
}
PRIVATE:
Static Singleton * ms_pinstance;
....
};
This is correct.
However, it is inconvenient to manually release the memory used by this object when the program exits! Can it be released by itself rather than manually? I thought of auto_ptr.
Class Singleton
{
// Private all Constructors
Public:
Singleton & getinstance (void)
{
If (ms_apinstance.get () = NULL)
Ms_apinstance.reset (New Singleton ());
Return * ms_apinstance;
}
PRIVATE:
Static STD: auto_ptr <Singleton> ms_apinstance;
....
};
The static object smart pointer will be analyzed after the program exits the main function, and the memory used by the object will be deleted. In this way, you can.
When I read 47 articles in Objective C ++, I got a simpler implementation method.
Class Singleton
{
// Private all Constructors
Public:
Singleton & getinstance (void)
{
Static Singleton instance;
Return instance;
}
....
};
The above Code uses the property of "local static objects of C ++ functions are initialized when objects are defined for the first time during function calling.
I want to write less code every time I write a single-key class, and define a macro:
# Define singleton_implement (classname )\
Public :\
Static classname & getinstance (void )\
{\
Static classname instance ;\
Return instance ;\
}
In this way, I want to define a single-key class, add the macro to the class declaration, and privatize all constructors to implement a single-key class.

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.