To design a class, we can only generate one instance of the class.

Source: Internet
Author: User
The main points of the singleton pattern are three; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own. From the specific implementation point of view, is the following three points: first, the Singleton mode class only provides a private constructor, the second is that the class definition contains a static private object of the class, and third, the class provides a static public function to create or get its own static private objects. #include <iostream> #include <stdlib.h>using namespace Std;class Csingleton{private:csingleton ()// Constructor privatization {cout<< "Csingleton ()" <<ENDL;} Public:static csingleton* getinstance () {static csingleton* m_pinstance;//determines if the first call to if (m_pinstance==null) {m_  Pinstance= new Csingleton ();} return m_pinstance;};    void Test () {csingleton* c=csingleton::getinstance (); csingleton* s=csingleton::getinstance ();} int main () {test (); System ("pause"); return 0;} Result: [Object Object]


This article from "Liveyoung" blog, reproduced please contact the author!

To design a class, we can only generate one instance of the class.

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.