Got an unexplained behaviour with the following:
Case 1:
a.cpp
compiled as a .dll library and used in main() of main.cpp
Bar b;
//constr
Bar::Bar(){
//... initialize members
}
//private library init
Bar::init(){ ...}
//public API init
bool lib_init(){
b.init();
}
From what I understand, this approach may fail due to undefined initialization behavior of globals.
Case 2:
a.cpp
compiled as a .dll library and used in main() of main.cpp
Bar* b;
//constr
Bar::Bar(){
//... initialize members
}
//private library init
Bar::init(){ ...}
//public API init
bool lib_init(){
b = new Bar;
b->init();
}
This time it works, when dynamic allocation is used.
Case 3 (most surprising)
a.cpp
compiled as a .dll library and used in main() of main.cpp
static Bar& getBarObj()
{
static Bar g_objBar;
return g_objBar;
}
//constr
Bar::Bar(){
//... initialize members
}
//private library init
Bar::init(){ ...}
//public API init
bool lib_init(){
getBarObj().init();
}
As opposed to case 1, where Bar obj instantiation might have been undefined, in case 3 it is used "upon request". Yet, case 3 provides same behaviour as case 1.
And my question is ... can anyone explain what's going on here? Everything is built with VC2008 Release mode (have no option for Debug mode for this proj)