D
dewdman42
I have a question for you C++ gurus. Let's say I have a class
singleton class such as:
class singleton
{
.
.
public:
singleton* getInstance();
private:
static singleton* __instance;
.
.
}
singeton.cpp:
singleton* singleton::__instance;
singleton* getInstance()
{
if (__instance==null) {
__instance = new singleton;
}
return __instance;
}
So far so good. Now the question is... some other code in some other
module decides to call singleton::getInstance() during app startup (ie,
before main()). The problem that I am seeing is that if the module
needs to call getInstance() before singleton.cpp has initialized the
static member variable "__instance" then what happens?
singleton class such as:
class singleton
{
.
.
public:
singleton* getInstance();
private:
static singleton* __instance;
.
.
}
singeton.cpp:
singleton* singleton::__instance;
singleton* getInstance()
{
if (__instance==null) {
__instance = new singleton;
}
return __instance;
}
So far so good. Now the question is... some other code in some other
module decides to call singleton::getInstance() during app startup (ie,
before main()). The problem that I am seeing is that if the module
needs to call getInstance() before singleton.cpp has initialized the
static member variable "__instance" then what happens?