????local static
???????????д??????????main????????????getInstance?????????????????main???????????????
??????????????е???????????????????local static??????C++?????????????local static?????????????????????????????????????????????????
template<typename T>
class Singleton
{
private:
Singleton();
~Singleton();
class Creater
{
public:
Creater()
: value_(new T())
{
}
~Creater()
{
delete value_;
value_ = NULL;
}
T& getValue()
{
return *value_;
}
T* value_;
};
public:
static T& getInstance()
{
static Creater creater;
return creater.getValue();
}
private:
class Dummy
{
public:
Dummy()
{
Singleton<T>::getInstance();
}
};
static Dummy dummy_;
};
template<typename T>
typename Singleton<T>::Dummy Singleton<T>::dummy_;
???????????????dummy_????????????main????????е???getInstance????????????????????????????main?????????????Singleton???????????????????main???????????????г????????????????
???????????????????main?????????????????????????2????????????
?????????????????????????д???У????Щ??????main?????????á????Щ??????main???????????????????????main??????????????????2?????????????????????д????????????????????????????????????????ò???????main?????????Singleton????????????е??????????????staitc member????C++???????static member?????main????????????????????????????static member????????????δ?????? ?????????main??????getInstance??????п?????????Singleton??static member????г???????????????
?????????????main?????Singleton???????????main???????????????????????????£?????????????????д?????C++ 11?μ?Meyers Singleton????????g++ 4.0????????汾???????????????????????????μ?C++ 03????????