생성 패턴: 5. Singleton(단일체)

Posted on 2009. 5. 12. 12:40
Filed Under Programming/Design Pattern

//---------------------------------------------------------------------------//
// FileName: Singleton.h
// Description : Singleton template class
//---------------------------------------------------------------------------//
#if	!defined(__SINGLETON_H__)
#define __SINGLETON_H__

// Singleton class
#pragma warning (disable : 4311)
#pragma warning (disable : 4312)
template class CSingleton
{
	// Constructor & Destructor
public:
	CSingleton()
	{
		int nOffset = (int)(T*)1 - (int)(CSingleton *)(T*)1;
		sm_pInst = (T*)( (int)this + nOffset );
	}
	~CSingleton() { sm_pInst = NULL; }

	// Attributes
private:
	static T *sm_pInst; // Instance pointer

	// Implementation-Functions
public:
	static T* Inst()
	{
		return sm_pInst;
	}
};


template  T* CSingleton ::sm_pInst = NULL;

#pragma warning (default : 4311)
#pragma warning (default : 4312)

#endif	//__SINGLETON_H__
반응형

About

by 쑤기c

반응형