1

I'm new to programing and I was trying for the program which makes the class singleton..Is this the correct approach for making a class singleton??

#include <iostream> using namespace std; class Singleton { private: static bool instanceFlag; static Singleton *single; public: static Singleton* getInstance(); void method(); ~Singleton() { instanceFlag = false; } }; bool Singleton::instanceFlag = false; Singleton* Singleton::single = NULL; Singleton* Singleton::getInstance() { if(! instanceFlag) { single = new Singleton(); instanceFlag = true; return single; } else { return single; } } void Singleton::method() { cout << "Method of the singleton class"; } int main() { Singleton *sc1,*sc2; sc1 = Singleton::getInstance(); sc1->method(); sc2=Singleton::getInstance(); sc2->method(); return 0; } 

Is this the correct way of making class singleton??

5
  • there are lots of examples: stackoverflow.com/questions/1008019/… Commented Dec 13, 2016 at 16:38
  • related/dupe: stackoverflow.com/questions/1008019/c-singleton-design-pattern Commented Dec 13, 2016 at 16:38
  • first thing to note, the constructor needs to be private otherwise you're not forcing users to call getInstance Commented Dec 13, 2016 at 16:38
  • The usefulness of instanceFlag seems pointless, as you already have one of those; the null or non-null single pointer itself (the usefulness of which also being questionable, as a static var in the local getInstance() member would facilitate most of this). Commented Dec 13, 2016 at 16:39
  • As a rule of thumb, I've found it's better to have the impl and the singleton-ness separate. i.e. class MyEpicClass{}; class MyEpicClassSingleton{} which means that when you can work out how to remove the singleton, everything that was working with a MyEpicClass will require no editing. Commented Dec 13, 2016 at 17:17

2 Answers 2

3

You are over-complicating things. Try the Scott Meyers singleton:

struct SingletonClass { // instance() function return a reference static SingletonClass& instance() { // static local variable are initialized one time. static SingletonClass inst; // We return the instance, which is the same for every calls. return inst; } private: // Private since we don't want other class to create instances SingletonClass() = default; // Our class is not copiable or movable SingletonClass(const SingletonClass&) = delete; SingletonClass(SingletonClass&&) = delete; SingletonClass& operator=(const SingletonClass&) = delete; SingletonClass& operator=(SingletonClass&&) = delete; }; 

You can use your class like that:

auto& myInstance = SingletonClass::instance(); 

Bonus: It doesn't use dynamic allocation, it's thread safe, and is much simpler.

Sign up to request clarification or add additional context in comments.

Comments

1

try this solution:

class Singleton { private: static Singleton* instance; Singleton() {} // must be private public: static Singleton* getInstance() { if (instance == NULL) instance = new Singleton(); return instance; } void method() { cout << "Method of the singleton class\n"; } }; Singleton* Singleton::instance = NULL; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.