singleton pattern in c++

Microsoft Windows xp professional w/ sp2
June 1, 2010 at 09:53:20
Specs: Windows XP, 512mb
how can we create a class having only one object using singleton pattern?
i searched on web and found this but couldn't understand it.

[/*
Creational Pattern: SINGLETON
Author: Rajesh V.S
Language: C++
Email: rajeshvs@msn.com
*/

#include <iostream>


using namespace std;

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;
}


can you please explain this or make a simple program for class emp restricting only one object emp1 please.
thank you


See More: singleton pattern in c++

Report •

#1
June 1, 2010 at 10:46:41
You already have such an example. It's in function main() in the code you posted. sc1 and sc2 are the same object.

Report •

#2
June 2, 2010 at 03:16:41
yes but can you explain this program step by step with comments am just a beginner in c++ or please write a simpler program

Report •

#3
June 2, 2010 at 15:42:45
Ok.

class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;

These are data member variables, and static means that the variables belong to the whole class and not to each object, i.e. there is only one instanceFlag variable in the whole application. These variables are in the private section so they are encapsulated. A user of this class can only access these variables through an accessor method that the class may provide.

Singleton()
{
//private constructor

}

A private constructor means that users of this class cannot instantiate objects using "new Singleton" or simply on the stack.

public:
static Singleton* getInstance();

The getInstance method is public, so can be used by other functions. It is the only way that others can create a Singleton object. They cannot just declare it normally, since its constructor is private.

void method();

Just an example method.

~Singleton()
{
instanceFlag = false;
}

This destructor is not really necessary here.

};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}

getInstance checks to see if a singleton object already exists. If it doesn't it creates one. If it exists, it just returns a pointer to it.

And now here is a use case:

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

This just shows that although you have two separate Singleton pointers, they both point to the same object, returned by getInstance.

There are many different ways that a Singleton can be implemented in C++. If you only look at one implementation, such as the one above, you will not get the full benefit of understanding its uses and its pitfalls. I recommend a good text book, such as "Accelerated C++" by Koenig & Moo, as well as the classic Design Patterns book by Gamma et al.


Report •

Related Solutions

#4
June 2, 2010 at 16:59:46
Singletons do two things.
1) They give you global access to an instance of an object.
2) They enforce a limit of one instance of an object.

If you need the former, just create a global instance of the object.

If you need the latter, be prepared to explain how two instances of your class will cause the Apocalypse.

If you want the former, you probably don't need it, and you should read a good book on design patterns.

If you want the latter, you should probably read that book anyways.

(All) that said, that example is pretty basic. If you're having a hard time reading it, maybe you should get a good book on the subject.


Report •

#5
June 3, 2010 at 08:21:05
thanks it helped a lot.

Report •

Ask Question