G
Guest
Im getting a runtime error because Ive got a static object that is not
properly initialized.
---------- header file xxx.hpp ---------------
class SomeClass {
SomeClass() { .. constructor here ..;}
} // end SomeClass
static SomeClass staticObject; // declaration in header
-------------- in .cpp file: ----
SomeClass staticObject; // definition in .cpp file: constructor called by
loader
When the code is executed at run time, and the static object staticObject
is accessed, I get a failure becuase it's constructor has not been executed
yet (so its data members are not initialized).
Another programmer on my team says that statics (and globals) are okay for
integers, floats, simple arrays (of ints, floats), char[] strings, and for
classes (or structs) that DO NOT have a constructor.
But, he says, one should never use a static/global for an object of a class
that has a constructor, because the loader is very unpredictable, and you
cannot be sure the loader will call the static's constructor before some
software tries to use/access the static.
Can anyone confirm this? Does anyone else have problems using
statics/globals of classes that have constructors.
PS: I should mention that this software is in a DLL (not an application)
that is attached to processes at run time, not link time.
properly initialized.
---------- header file xxx.hpp ---------------
class SomeClass {
SomeClass() { .. constructor here ..;}
} // end SomeClass
static SomeClass staticObject; // declaration in header
-------------- in .cpp file: ----
SomeClass staticObject; // definition in .cpp file: constructor called by
loader
When the code is executed at run time, and the static object staticObject
is accessed, I get a failure becuase it's constructor has not been executed
yet (so its data members are not initialized).
Another programmer on my team says that statics (and globals) are okay for
integers, floats, simple arrays (of ints, floats), char[] strings, and for
classes (or structs) that DO NOT have a constructor.
But, he says, one should never use a static/global for an object of a class
that has a constructor, because the loader is very unpredictable, and you
cannot be sure the loader will call the static's constructor before some
software tries to use/access the static.
Can anyone confirm this? Does anyone else have problems using
statics/globals of classes that have constructors.
PS: I should mention that this software is in a DLL (not an application)
that is attached to processes at run time, not link time.