
20 Mar
2009
20 Mar
'09
5:53 a.m.
Hi, Some time, the sequence of static variable initialization always make something bad. the piece of code below will useful about this problem. class static_ { public: template <int N, class T> static T& var() { static T instance; return instance; } private: ~static_() {} }; so, if a static variable of std::string is wanted, I will use: static_::var<0, std::string>() = ":)"; the first template parameter is used for when many static variable of same type is needed. most important thing is all the variable will be initialized before use. Just for fun. Dongfei.