C ++ Singleton mode is also called Singleton mode and Singleton mode. The Singleton mode ensures that a class has only one instance and provides a global access point for it. This instance is shared by all program modules. Such functional modules are needed in many places, such as system log output.
The C ++ Singleton mode has many implementation methods. In C ++, you can even use a global variable to do this, but such code is not elegant. The design pattern book provides a very good implementation that defines a singleton class and uses the Private Static pointer variable of the class to point to the unique instance of the class, obtain the instance using a public static method. The following class definitions
- Class CSingleton:
- {
- // Other members
- Public:
- Static CSingleton * GetInstance ()
- {
- If (M_pInstance= NULL)
- M_pInstance=NewCSingleton ();
- Return m_pInstance;
- }
- Private:
- CSingleton (){};
- Static CSingleton * m_pInstance;
- }
The Singleton class CSingleton has the following features:
◆ It has a static pointer m_pInstance for a unique instance and is private.
◆ It has a public function that can obtain this unique instance and create it as needed.
◆ Its constructor is private, so that you cannot create instances of this class elsewhere.
Most of the time, this implementation will not cause problems. Experienced readers may ask when the space pointed to by m_pInstance will be released? The more serious problem is, when will the instance's destructor be executed? If necessary operations are required in the class destructor, such as closing the file and releasing external resources, the code shown above cannot meet this requirement. We need a method to delete the instance normally.
You can call GetInstance at the end of the program and call the delete operation on the returned pointer. This can implement functions, but it is not only ugly, but also prone to errors. Because such additional code is easy to forget, and it is difficult to ensure that after the delete operation, no code can call the GetInstance function.
A proper method is to let the class know how to delete itself when appropriate. In other words, you can mount the delete operation to a proper point in the system so that the operation can be automatically executed at the appropriate time. We know that at the end of the program, the system will automatically analyze all global variables. In fact, the system will analyze the static member variables of all classes, just like these static members are also global variables.
With this feature, we can define such a static member variable in the singleton class, and its only job is to delete the singleton class instance in the destructor. In the following code, the CGarbo class Garbo stands for spam workers ):
- Class CSingleton:
- {
- // Other members
- Public:
- Static CSingleton * GetInstance (){...}
- Private:
- CSingleton (){};
- Static CSingleton * m_pInstance;
- Class CGarbo // Its only job is to delete the CSingleton instance in the destructor.
- {
- Public:
- ~ CGarbo ()
- {
- If (CSingleton: m_pInstance)
- Delete CSingleton: m_pInstance;
- }
- };
- Static CGarbo Garbo; // defines a static member. At the end of the program, the system calls its destructor.
- }
The C ++ Singleton mode is defined as a private embedded class of CSingleton to prevent this class from being abused elsewhere.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code