I am long in Java and relatively new to C++. So in Java if there is some complex static object at class level (well in Java everything is at class level), we could simply use a static block to initialize it. E.g.
public class MyClass extends MyBase {
public static final Map<String, AComplexClass> STATIC_MAP = new HashMap<String, AComplexClass> ();
static {
AComplexClass first = ComplexClassFactory.createComplexType1();
first.configure("Something", "Something");
STATIC_MAP.put("key1", first);
// do some more
}
}
However given the limitation for initializing C++ static variables, this might be too complex if I wanna define the same STATIC_MAP
, which seems I need to have named variables in the .cpp
file for each AComplexClass
object and then put to the map, creating quite some rubbish in the code and also chance for other classes to refer to them accidentally.
So what's the best practice for C++ equivilent of such static initialization logic? I assume this is the same strategy to use but maybe some other style.