2022年8月31日
c++单例模式模板类
singleton.h
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
template<typename T>
class Singleton
{
public:
static T* Instance() {
static T instance;
return &instance;
}
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton) = delete;
protected:
Singleton() {}
virtual ~Singleton() {}
};
#endif //!__SINGLETON_H__
示例:
class LogUtil : public Singleton<LogUtil>
{
public:
LogUtil();
virtual ~LogUtil();
int Log(std::string str);
}
LogUtil::Instance()->Log("单例模式");