template<class T>
class Singleton
{
public:
Singleton()
{
LordAssert(!ms_pSingleton);
ms_pSingleton = static_cast<T*>(this);
}
~Singleton()
{
LordAssert(ms_pSingleton);
ms_pSingleton = NULL;
}
private:
/** \brief Explicit private copy constructor. This is a forbidden operation.*/
Singleton(const Singleton<T>&);
/** \brief Private operator= . This is a forbidden operation. */
Singleton& operator = (const Singleton<T>&);
public:
static T* Instance()
{
return ms_pSingleton;
}
protected:
static T* ms_pSingleton;
};
template<class T>
T* Singleton<T>::ms_pSingleton = NULL;
单例模式用继承的方式在工程中的应用
于 2022-04-09 14:48:45 首次发布