"Design mode" singleton mode

Source: Internet
Author: User

1. Meyers Singleton

  

1ClassSingleton {2Public:3Static singleton&GetInstance () {4StaticSingleton instance;5ReturnInstance6}78Private:9Singleton () {}Ten Singleton (const singleton&); 11 singleton& operator= ( const singleton&); 12 }; 13 14 int main () 15 {16 Singleton &s = Singleton::getinstance (); 18 return 0; 19}             

This is the way Scott Meyers Singleton in "effective C + +". It is a lazy pattern, that is, when the class instance is first invoked, a new instance of that class is generated, and only this instance is returned at a later time.

The disadvantage of Meyers Singleton is the problem of thread safety. It is thread-safe after c++11. However, the standard before c++11 does not have thread safety.

  

"Design mode" 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.