单例模板类:
template <class T>
class Singleton
{
public:
Singleton()
{
}
virtual ~Singleton()
{
}
static T* getInstance()
{
if(!val)
val = new T;
return val;
}
static void release()
{
if (val)
delete val;
val = 0;
}
private:
static T* val;
};
template<class T>
T* Singleton<T>::val=NULL; //初始化
使用方法:(继承此模板类即可)
class Manager : public Singleton<Manager>
{
public:
void test();
private:
int i;
}
此时Manager 类为单例类
网友评论