Singleton 单例模板

时间:2022-02-22 09:45:24
 // singleton.h

 #ifndef SINGLETON_H
#define SINGLETON_H // 单例基类模板
template <class T>
class Singleton
{
public:
static T& give_me()
{
static T s_inst;
return s_inst;
} private:
// 禁止实现拷贝构造与拷贝赋值函数
explicit Singleton(const Singleton<T> &rhs);
Singleton<T>& operator = (const Singleton<T> &rhs); protected:
explicit Singleton() {}
virtual ~Singleton() {}
}; #endif // SINGLETON_H
 #ifndef TEST_MANAGER_H
#define TEST_MANAGER_H #include "singleton.h" class TestManager : public Singleton<TestManager>
{
friend class Singleton<TestManager>; private:
explicit TestManager();
virtual ~TestManager(); public:
void func();
}; #endif // TEST_MANAGER_H