Analysis on control and release of C ++ Singleton mode objects
Source: Internet
Author: User
Singleton mode is also called Singleton mode and Singleton mode. Use Singleton mode, Ensure that only one class exists. Instance And provides a global access point to access it. , The instance is owned Program Module sharing . Such functional modules are needed in many places, such as system log output. Singleton mode has many implementation methods. In C ++, you can even use a global variable to do this. Code 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 definition: Class Csingleton :{ // Other members Public : StaticCsingleton * getinstance () { If(M_pinstance = NULL) M_pinstance =NewCsingleton (); ReturnM_pinstance; } Private : Csingleton (){}; StaticCsingleton * m_pinstance; } The Singleton class csingleton has the following features: Ø It has a static pointer to a unique instance. M_pinstance, which is private. Ø It has a public function that obtains the unique instance and creates the instance 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
Some classes have static member variables, 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
. The cgarbo class in the following code (Garbo stands for spam workers ): Class Csingleton :{ // Other members Public : StaticCsingleton * getinstance (){ ... } Private : Csingleton (){}; StaticCsingleton * m_pinstance; ClassCgarbo// Its only job is to delete the csingleton instance in the destructor. { Public: ~ Cgarbo () { If(Csingleton: m_pinstance) DeleteCsingleton: m_pinstance; } }; StaticCgarbo Garbo;// Defines a static member. At the end of the program, the system will call its destructor. } Class cgarbo is defined as the private embedded class of csingleton to prevent this class from being abused elsewhere. At the end of the program running, the system calls the Garbo destructor, a static member of csingleton. This destructor deletes the unique instance of a single instance. Releasing a singleton object in this way has the following features: Ø Define the exclusive nested class within the singleton class. Ø Define Private Static members dedicated for release in the singleton class. Ø Use the program's global variable analysis feature at the end to select the final release time. Ø The Singleton Code does not require any operation and does not need to care about the release of objects.
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