The following is the simple example to create singleton class in c++
important steps in creating singleton class are as follows
Make constructor private
Have an instance of the class inside the class itself
Have a static public function to get the instance declared inside the class
class Singleton
{
private :
int x;
static Singleton* s;
Singleton(int n)
{
x=n;
}
public :
static Singleton getInstance(int n)
{
if(s==NULL)
s=new Singleton(n);
return *s;
}
//....
};
Singleton Singleton::s=NULL;
int main()
{
Singleton object=Singleton::getInstance(10);
//...
}