A simple example to sigleton class.
/* A Singleton class example
* Date : 24-Feb-2004
* Author: Prakhar Dubey: prakharprakhar@gmail.com
*
*/
//////////////////////////////////////////////////////
#include<iostream>
using namespace std;
class Singleton
{
private:
static Singleton *singleInstance;
Singleton()
{
cout << "Class creating once" << endl;
}
public:
static Singleton* getInstance()
{
if(!singleInstance)
singleInstance = new Singleton();
return singleInstance;
}
};
Singleton *Singleton::singleInstance = 0;
int main()
{
Singleton::getInstance();
Singleton::getInstance();
}
//////////////////////////////////////////////////////
OUTPUT:
Class creating once
No comments:
Post a Comment