好的单例模型
1.
static ScriptingCore *getInstance() {
static ScriptingCore* pInstance = NULL;
if (pInstance == NULL) {
pInstance = new ScriptingCore();
}
return pInstance;
};
真正做到只能调用getInstance访问数据。
2.
template<typename T>
class Singleton
{
static T* ms_singleton;
public:
Singleton()
{
ENSURE( !ms_singleton );
ms_singleton = static_cast<T*>(this);
}
~Singleton()
{
ENSURE( ms_singleton );
ms_singleton = 0;
}
static T& GetSingleton()
{
ENSURE( ms_singleton );
return *ms_singleton;
}
static T* GetSingletonPtr()
{
ENSURE( ms_singleton );
return ms_singleton;
}
static bool IsInitialised()
{
return (ms_singleton != 0);
}
};
template <typename T>
T* Singleton<T>::ms_singleton = 0;
使用模板编程,貌似编码更简便点,当没有1好理解