The simplest way to implement singleton (single case mode) in C + + __c++

Source: Internet
Author: User

A few days ago to see the Java implementation of the Singleton, I would like to achieve in C + +, looking for a lot of information, looked at the different versions of cattle, but finally found a stack overflow on the simplest way, do not need to determine whether there are instances exist, In the case of multithreading can also be used normally, now posted for reference:

Class S
        static s& getinstance ()
            static S    instance; 
            return instance;
        S () {};                   Constructor
        s (S const&);              Don ' t implement
        void operator= (S const&);//don ' t implement
It actually uses the static variables of the member functions in C + +: Static local variables are initialized the first time they are used and are not destroyed until the program exits.

Specific information:

Here are the four kinds of singleton I've summed up in C + +:

Singleton.cpp:Defines the entry point for the console application. 

#include "stdafx.h" #include <iostream> #include <stdio.h> #include <Windows.h> using namespace std;
	C + + Singleton Version 1 class Singleton1 {Private:singleton1 () {};
	static singleton1* instance;
	Virtual ~singleton1 (void) {} Public:int ia;
		Static singleton1* getinstance () {if (NULL = = Instance) {instance = new Singleton1 ();
	return instance;

Must define static member, not just declaration singleton1* singleton1::instance = NULL;
C + + Singleton Version 2//use Smart point to release memory #include <memory> using namespace std; Class Singleton2 {public:static Singleton2 * getinstance () {if (NULL = = Instance.get ()) {Instance.reset (new S)
		Ingleton2 ());
	return Instance.get ();
int ia;
Private:static shared_ptr<singleton2> instance;

Shared_ptr<singleton2> singleton2::instance; C + + Singleton Version 3//use template To Reduce some duplicate work template <class t> class Singleton {public:static t* getinstance (); private:single ton () {} ~singleton () {} Singleton (const singleton&) {} singleton& operator= (const singleton&) {} static Sha
red_ptr<t> instance;


Template <class t> shared_ptr<t> singleton<t>::instance; Template <class t> t* singleton<t>::getinstance () {if (NULL = Instance.get ()) {Instance.reset (new single
	ton ());
return Instance.get (); 
		}//c++ Singleton Version 4//avoid memory allocation class Singleton4 {public:static singleton4& getinstance () {
		Guaranteed to be destroyed.
		instantiated on the. Static variable lifetime in function-
		What-is-the-lifetime-of-a-static-variable-in-a-c-function static Singleton4 instance;
	return instance; Private:singleton4 () {} ~singleton4 () {}//Dont forget to declare these two. You are want to make sure they// are unaccessable otherwise you accidently get copies of//your singleton.

Singleton4 (Singleton4 const&);//don ' t implement void operator= (Singleton4 const&);//don ' t implement};
	int _tmain (int argc, _tchar* argv[]) {/* version 1:memory leak a = singleton1* ();
	A->ia = 100;
	Singleton1* B = singleton1::getinstance ();
	cout << B->ia << Endl;
	* * singleton2* a2 = singleton2::getinstance ();
	A2->ia = 200;
	singleton2* b2 = Singleton2::getinstance ();
	cout << B2->ia << Endl;

	OutputDebugString (_t ("Hello world!"));

	System ("pause");
return 0;

Related Article

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: 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.