1.写一个头文件(仅头文件)
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
template <typename T>
class Singleton
{
public:
static T* getInstance()
{
static T s_instance;
return &s_instance;
}
Singleton(T&&) = delete;
Singleton(const T&) = delete;
void operator= (const T&) = delete;
protected:
Singleton() = default;
virtual ~Singleton() = default;
};
#endif
2.创建一个类继承自上面的类,记得要提供模版参数
class FileFunction : public Singleton<FileFunction>
{
public:
xxx
};
3.调用时,直接通过FileFunction来调用静态方法:FileFunction::getInstance().