[Design mode] [C + +] Singleton mode

Source: Internet
Author: User

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

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.