简单的单例模板,继承使用即可

template <class T>
class FvSingleton
{
protected:
static T *ms_pkInstance;

public:
FvSingleton()
{
CCASSERT(ms_pkInstance == NULL,"");
ms_pkInstance = static_cast< T * >( this );
}

virtual ~FvSingleton()
{
ms_pkInstance = 0;
}

static T &Instance()
{
if (ms_pkInstance == nullptr)
{
new T;
}
return *ms_pkInstance;
}

static T *pInstance()
{
return ms_pkInstance;
}
};

 

#define FV_SINGLETON_STORAGE( TYPE ) \
template <> \
TYPE * FvSingleton< TYPE >::ms_pkInstance = 0; 

posted @ 2018-01-08 14:20  杨小聪  阅读(291)  评论(0编辑  收藏  举报