Constant Being Initialized AFTER Variable That Uses It
I am having a problem with a constant I am defining in MSVC++ 6.0.
Basically, I declare a constant string exists in Test.h.
extern const std::string strTest;
In a cpp (other.cpp) file, I place the definition for strTest. It is marked as extern because without it, the constant would only be marked for internal linkage and results in a compiler error saying that strTest was never defined.
extern const std::string strTest = "Test";
In another cpp file (MyClass.cpp), I have a static member variable of a class. The declaration for the variable exists in MyClass.h. In MyClass.cpp I define the variable like so.
std::string MyClass::strPath = strTest;
However, when I run the code, strPath is filled with "" instead of "Test". I placed breakpoints on the strTest and strPath lines of code to see when each was occuring. I was suprised to find that the strPath line runs prior to the strTest line, and thus was initializing strPath with an uninitialized value of strTest. Is this expected behavior? I was under the impression that constants should be initialized before anything else.
Is there anyway to solve this problem to ensure that every constant defined in such a way is initialized before any attempt is made to use it? Thank you for any input you have.