Where is QuestionName C1Q1:::question_name_
located relative to ClassTemplate::questionName_questionPointer_map_
? They seem to be both variables with static storage duration, i.e., they are constructed before main()
is run. However, the C++ compiler/linker orders the construction of such global objects only with one translation unit (in which case the objects are constructed top to bottom), not between translation units (in which case the objects are constructed in a random order).
You problem looks as if ClassTemplate::questionName_questionPointer_map
would be constructed after C1Q1::question_name_
. That is, when C1Q1::question_name_
is constructed, an object which is not, yet, constructed is being accessed.
The conventional fix is to make the static
object other objects depend on not an object but rather a function with a local static
variable to which a reference is returned:
std::map<QuestionName,ClassTemplete *>&
ClassTemplete::questionName_questionPointer_map_() {
static std::map<QuestionName,ClassTemplete *> rc;
return rc;
}
(note that this construction is not thread-safe when you don't use C++11; it is thread-safe when using C++11).