方法一:
class Instance2{
public:
static Instance2& GetInstance() {
static Instance2 ins;
return ins;
}
private:
Instance2(){};
};
方法二(基于C++11):
template<typename T>
class Instance{
public:
static T& GetInstance() {
static std::once_flag flag_;
static T* ptr_ = nullptr;;
std::call_once(flag_, [&](){
ptr_ = new T();
});
return *ptr_;
}
private:
};
class test :public Instance < test > {
friend class Instance < test > ;
test(){};
};