It seems from the above that
* Only someFunc (plus possibly functions called by someFunc) uses
the
var.
Thats not correct unfortunately. The static variable is used to make a
list of all classes of a certain type.
* 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).
The pointer was an attempt to trick the initialization order thing. If
the vector is not a pointer, and it is accessed before it is
initialized, it actually crashes - with the pointer at least it doesnt
crash (though im assuming it is definately not a correct solution).
I have given this a bit more thought and came up with this:
class MyClass
{
public:
static void myfunc();
private:
static std::vector<int>& getvec() { static std::vector<int> v;
return
v; };
};
}
now myfunc can simply do
void MyClass::myfunc()
{
std::vector<int>& myvec=getvec();
// do sth with myvec
}
That seems like a viable method to circumvent the problem, right?
Sure. In this case you're relying on the behaviour that static local
definition. (Which is Standard behaviour.)