a8/a8/singleton.h
2018-08-26 20:34:01 +08:00

39 lines
776 B
C++

#ifndef A8_SINGLETON_H
#define A8_SINGLETON_H
namespace a8
{
template<class T>
class Singleton
{
public:
static T* Instance()
{
if (!instance_) {
// 二次检查
if (!instance_) {
instance_ = std::shared_ptr<T> (new T());
}
}
return instance_.get();
}
protected:
Singleton() {} //防止实例
Singleton(const Singleton&) {} //防止拷贝构造一个实例
Singleton& operator=(const Singleton&){} //防止赋值出另一个实例
~Singleton() {}
private:
static std::shared_ptr<T> instance_;
};
template<class T> std::shared_ptr<T> Singleton<T>::instance_;
}
#endif