Reference: http://blog.csdn.net/hackbuteer1/article/details/7460019
The singleton mode intent is to ensure that a class has only one instance, and provides a global access point to access it, which is shared by all program modules. There are many places need such a function module, such as the log output of the system, the GUI application must be a single mouse, the modem connection requires one and only need a phone line, the operating system can only have a window manager, a PC with a keyboard.
The singleton pattern manages its only instance through the class itself, which provides a way to solve the problem. The only instance is a generic object of the class, but when you design the class, it allows it to create only one instance and provide global access to the instance. The unique instance class singleton hides the operation that created the instance in a static member function. It is customary to call this member function instance (), whose return value is a pointer to a unique instance.
classcsingleton{Private: Csingleton ()//The constructor is private { } StaticCsingleton *m_pinstance; Public: StaticCsingleton *getinstance () {if(m_pinstance = = NULL)//determines whether the first call isM_pinstance =NewCsingleton (); returnm_pinstance; }};
Attention:
I'm in vs because I don't know why so far
Must be declared again in the CPP file:
Static Csingleton * csingleton::m_pinstance = NULL;
I don't know why ... But this is going to work.
[Design mode] [C + +] Singleton mode