Static order of initialization
Static variables are variables which are declared only once, essentially existing in a global scope, though potentially only shared between instances of a particular class. It's also possible to have classes which are completely static:
class Foo { static std::map<int, std::string> strings; static std::string oneString; public: static void init(int a, std::string b, std::string c) { strings.insert(std::pair<int, std::string>(a, b)); oneString = c; } }; std::map<int, std::string> Foo::strings; std::string Foo::oneString;
As we can see here, static variables along with static functions seem like a very simple, yet powerful concept. While at its core this is true, there's a major issue which will catch the unwary when it comes to static variables and the initialization of classes. This is in the form of initialization order.
Imagine what happens if we wish to use the preceding class from another class...