Re: static member initialization
* Frank Neuhaus:
Perhaps you're running into the issue of the indeterminate order in which
globals are initialized between translation units?
Are you trying to access the the static variable before main() starts?
Mhhh I think that may be it. It is initialized from a global variable
definition (static int something=MyClass::someFunc() ) somewhere else in the
code. Say Iam doing it like this:
std::vector<int>* MyClass::myStaticVar=0;
int MyClass::someFunc()
{
if (!myStaticVar)
myStaticVar=new std::vector<int>();
// do sth with myStaticVar
}
Now if myStaticVar is initialized after the someFunc is called, the pointer
in myStaticVar could be overwritten by the initialization, right?
So what if I do:
std::vector<int>* MyClass::myStaticVar;
Will it also get overwritten?
Is there any "safe" way to achieve the desired behaviour?
It seems from the above that
* Only someFunc (plus possibly functions called by someFunc) uses the var.
* The use of a pointer is not from any particular reason, i.e. it's not in
order to circumvent ordinary lifetime rules (destruction order).
And given that, simply do
int myClass::someFunc()
{
static std::vector<int> v;
// Do something with v
}
If that doesn't work it might help to describe the "desired behavior".
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?