0

I am good at C++, but trying to understnd how singleton works. So I was looking into the code or article HERE .

If I will see the code;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {/*private constructor*/}
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" << endl;}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;
}

In this above code that prints Method of the singleton class twice. If we wanted to print the output twice, then why do we need singleton. We can write like;

sc1->method();
sc2->method();

Why do need such a complicated code as written above. One thing I noticed, instanceFlag is becoming true once condition satisfies with onject sc1 but when the object sc2 gets called then it goes to else part.

So, what exactly we are trying to do here?
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • That's a really bad singleton implementation for c++. Look here: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289#1008289 – Martin York Nov 19 '12 at 09:09

3 Answers3

3

You might want to read up wikipedia to understand what Singleton means

Essentially, for situations where you want to have only 1 object of a class at any time, you use such a pattern

In this code, Singleton::getInstance() is meant to return this 1 object of the class. The first time you call it, the object is constructed. Subsequent calls to Singleton::getInstance() will be returned the same object. instanceFlag tracks if the object has been instantiated.

Another hint is that the constructor is private, this means there is NO way to get an instance of the class except by the GetInstance() function.

p.s. I do agree with you though, this code is more convoluted than im used to.. It also (technically) has a memory leak .. the instance is never deleted

This is what i see more often

static Singleton* getInstance()  
{  
    static Singleton instance;  
    return &instance;  
}  
Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

As the name suggests Singleton design patter is used when we need at max one object of a class.

Below is a simple explanation of how singleton is achieved.

Sometimes we need to declare a class which allows creation of only one instance of it .

Suppose you are creating a media player of your own and you want it to behave like windows media player,which as we all know, allows only one instance to be created and it would would not be possible to run two windows media players simultaneously. Technically speaking we can create only one object of windows media player. To implement this concept we can declare our class in the following way:

class media_player                                   // Class for creating media player
{
         static media_player player  =  null;             // static object of media_player which will be unique for all objects

         other static members...........                    // you can provide other static members according to your requirement

         private media_player()    // Default constructor is private 
         {

         }

        public  static createInstance( )      // This method returns the static object which will be assigned to the new object
        {
               if(player==null)                            // If no object is present then the first object is created
               {
                       player = new media_player();
               }
              return player;                               
       }

       other static functions..............    // You can define other static functions according to your requirement

}

If we create any object of this class then the staic object (player) will be returned and assigned to the new object.

Finally only one object will be there.

This concept is also used to have only one instance of mouse in our system.i.e. we can not have two mouse pointers in our system.

This type of class is known as singleton class.

Narendra
  • 3,069
  • 7
  • 30
  • 51
1

The point is not how many times something gets executed, the point is, that the work is done by a single instance of the the class. This is ensured by the class method Singleton::getInstance().

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • To add, you might want to cout some instance variable there. Set the variable using the sc1 reference and get the variable using sc2 reference. – Ravi Y Nov 19 '12 at 06:48