sundeepblue
3/5/2014 - 3:00 AM

singleton implementation in c++

singleton implementation in c++

#include <iostream>
#include <string>
using namespace std;

class singleton {
private:
	static singleton* instance;
	singleton() {}
	singleton(const singleton &c) {}
	singleton& operator=(const singleton &c) {}
public:
	static singleton* getInstance() {
		if(instance == NULL) {
			instance = new singleton;
		}
		return instance;
	}
	void print_info() {
		cout << "created!\n";
	}
	/*  // a thread-safe way called DCLP  
		singleton* getInstance_DCLP() {
		if(instance == NULL) {
		Lock lock;
		if(instance == NULL)
		instance = new singleton;
		}
		return instance;
		}
	 */    
};

singleton* singleton::instance = NULL;   

int main()
{
singleton::getInstance()->print_info();
return 0;
}