template < typename T>
class TSingleton
{
public:
static T* Instance( void )
{
if (cpoInstance == NULL)
{
try{ cpoInstance = new T; } catch(...) { cpoInstance = NULL; }
}
return cpoInstance;
}
static T* Release(void)
{
delete cpoInstance;
cpoInstance = NULL;
}
private:
static T* cpoInstance;
};
template<typename T> T* TSingleton<T>::cpoInstance = NULL;
template<typename T> inline T* Instance(void)
{
return TSingleton<T>::Instance();
}
template<typename T> inline void Release(void)
{
TSingleton<T>::Release();
}
class MyApp
{
void cvFun(){}
...
};
int main()
{
Instance<MyApp>()->cvFun();
return 0;
}
Singleton模式
最新推荐文章于 2022-05-16 16:55:08 发布