Return to Snippet

Revision: 769
at May 1, 2010 18:53 by yuconner


Updated Code
//.h
class SingletonClass {

	static SingletonClass *singleton;
	SingletonClass();

public:
	static SingletonClass * get_singleton_instance();
	static void destroy();
	~SingletonClass();
};


//.cpp
SingletonClass *SingletonClass::singleton = NULL;

SingletonClass * SingletonClass::get_singleton() {
	if( singleton == NULL ) singleton = new SingletonClass();
	return singleton;
}

void SingletonClass::destroy() {
	if( singleton != NULL ) delete singleton;
	singleton = NULL;
}

SingletonClass::SingletonClass() {
	singleton = this;
}

SingletonClass::~SingletonClass() {
}

Revision: 768
at December 5, 2006 16:56 by yuconner


Updated Code
//.h
class SingletonClass {

	static SingletonClass *singleton;
	SingletonClass();

public:
	static SingletonClass * get_singleton_instance();
	~SingletonClass();
};


//.cpp
SingletonClass *SingletonClass::singleton = NULL;

SingletonClass * SingletonClass::get_singleton() {
	if( singleton == NULL ) singleton = new SingletonClass();
	return singleton;
}

SingletonClass::SingletonClass() {
	singleton = this;
}

SingletonClass::~SingletonClass() {
}

Revision: 767
at August 4, 2006 15:25 by yuconner


Initial Code
//.h
class SingletonClass {

	static SingletonClass *singleton;
	SingletonClass();

public:
	static SingletonClass * get_singleton_instance();
	~SingletonClass();
};


//.cpp
SingletonClass *SingletonClass::singleton = NULL;

SingletonClass * SingletonClass::get_singleton() {
	if( singleton == NULL ) singleton = new FilesConfig();
	return singleton;
}

SingletonClass::SingletonClass() {
	singleton = this;
}

SingletonClass::~SingletonClass() {
}

Initial URL

                                

Initial Description
Note: This doesn't work in multi-threaded environments, check http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B

Singleton class model
then:
SingletonClass *pSC = SingletonClass::get_singleton_instance();
pSC->any_public_function();
or: SingletonClass::get_singleton_instance()->any_public_function();

at the end of the program don't forget: SingletonClass::destroy();

Initial Title
Singleton class model

Initial Tags
design

Initial Language
C++