Create a gist now

Instantly share code, notes, and snippets.

Embed
Singleton example in C++
/*
* Example of a singleton design pattern.
* Copyright (C) 2011 Radek Pazdera
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
class Singleton
{
private:
/* Here will be the instance stored. */
static Singleton* instance;
/* Private constructor to prevent instancing. */
Singleton();
public:
/* Static access method. */
static Singleton* getInstance();
};
/* Null, because instance will be initialized on demand. */
Singleton* Singleton::instance = 0;
Singleton* Singleton::getInstance()
{
if (instance == 0)
{
instance = new Singleton();
}
return instance;
}
Singleton::Singleton()
{}
int main()
{
//new Singleton(); // Won't work
Singleton* s = Singleton::getInstance(); // Ok
Singleton* r = Singleton::getInstance();
/* The addresses will be the same. */
std::cout << s << std::endl;
std::cout << r << std::endl;
}
@yeweishuai

This comment has been minimized.

Show comment
Hide comment
@yeweishuai

yeweishuai May 4, 2017

If this is a header .h file, class implements should be written in .cpp file
Otherwise, compile error occurs.

yeweishuai commented May 4, 2017

If this is a header .h file, class implements should be written in .cpp file
Otherwise, compile error occurs.

@CMLDMR

This comment has been minimized.

Show comment
Hide comment
@CMLDMR

CMLDMR Oct 4, 2017

Good Example to show singleton. Thank you.

CMLDMR commented Oct 4, 2017

Good Example to show singleton. Thank you.

@oliviazqq

This comment has been minimized.

Show comment
Hide comment
@oliviazqq

oliviazqq Nov 22, 2017

thank you,very good

thank you,very good

@Rushi-Kumar

This comment has been minimized.

Show comment
Hide comment
@Rushi-Kumar

Rushi-Kumar Dec 1, 2017

how to implement in header only library.

how to implement in header only library.

@zhangxiao-ustc

This comment has been minimized.

Show comment
Hide comment
@zhangxiao-ustc

zhangxiao-ustc Jan 23, 2018

Is this implementation thread-safe?

Is this implementation thread-safe?

@BugLight

This comment has been minimized.

Show comment
Hide comment
@BugLight

BugLight Feb 11, 2018

Your implementation has a memory leak.

Your implementation has a memory leak.

@luca1337

This comment has been minimized.

Show comment
Hide comment
@luca1337

luca1337 Mar 10, 2018

nice example!

nice example!

@xintongc

This comment has been minimized.

Show comment
Hide comment
@xintongc

xintongc Mar 11, 2018

Very helpful! Thanks a lot!

Very helpful! Thanks a lot!

@ZOulhadj

This comment has been minimized.

Show comment
Hide comment
@ZOulhadj

ZOulhadj Mar 11, 2018

Nice, however, I just would say that there is a memory leak. When you create a raw pointer you need to make sure to delete it in the destructor. If not then make sure to new a smart pointer so it gets deleted automatically.

Nice, however, I just would say that there is a memory leak. When you create a raw pointer you need to make sure to delete it in the destructor. If not then make sure to new a smart pointer so it gets deleted automatically.

@OSinitsyn

This comment has been minimized.

Show comment
Hide comment
@OSinitsyn

OSinitsyn Mar 24, 2018

The copy constructor and the copy assignment operator should be declared private, i.e.

private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);

Otherwise, you will be able to clone your object. If you are using C++ 11, you may leave the copy constructor and the copy assignment operator public but explicitly delete them:

public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;

The copy constructor and the copy assignment operator should be declared private, i.e.

private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);

Otherwise, you will be able to clone your object. If you are using C++ 11, you may leave the copy constructor and the copy assignment operator public but explicitly delete them:

public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;

@ProgrammerXDesigner

This comment has been minimized.

Show comment
Hide comment
@ProgrammerXDesigner

ProgrammerXDesigner May 11, 2018

Nice, but I have some notes here:

  • First, you have memory leak.
  • And second, you should declare the copy constructor and the assignment operator of your class as private or delete them explicitly to prevent cloning your object.

Nice, but I have some notes here:

  • First, you have memory leak.
  • And second, you should declare the copy constructor and the assignment operator of your class as private or delete them explicitly to prevent cloning your object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment