Static Initialization Function in Classes

Content

Sometime when we write a C++ class, we want a static function is used to initialize the class. For example, a static function is used to initialize a static vector or map. We can do the same thing easily in Java, by Java’s static block. How we can do with C++?

At first, let’s see how to initialize a normal static member in C++.

class Test1
{
    public:static string emptyString;
};
string Test1::emptyString = "";
// also can be
// string Test1::emptyString;
// string Test1::emptyString("");

Continue reading