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??

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
berry
  • 29
  • 3
  • there are lots of examples: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern?rq=1 – EdChum Dec 13 '16 at 16:38
  • related/dupe: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – NathanOliver Dec 13 '16 at 16:38
  • first thing to note, the constructor needs to be private otherwise you're not forcing users to call `getInstance` – EdChum Dec 13 '16 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). – WhozCraig Dec 13 '16 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. – UKMonkey Dec 13 '16 at 17:17

2 Answers2

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.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
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;
ryonts
  • 126
  • 6