S
Spacen Jasset
Since it says in the standard that the order of initialization of
variables is indeterminate, does this also apply to a static within a
function?
e.g.
int GetInt()
{
static int test = 458;
return test;
}
when might test be initialized? Is it indeterminate like any other
object. In other words calling GetInt() before main() is executed
doesn't guarantee test has been initialized?
If this is the case how does one avoid problems with static
initialisation. For instance if I have a class A
class A
{
MyMutex mutex;
};
A a_object;
Here, let us say that MyMutex's constructor will read a static member
variable within the MyMutex class. Presumably, this is undefined
behavior since the static variable may not yet have been initialised.
If that is the case how do you avoid this sort of trap?
variables is indeterminate, does this also apply to a static within a
function?
e.g.
int GetInt()
{
static int test = 458;
return test;
}
when might test be initialized? Is it indeterminate like any other
object. In other words calling GetInt() before main() is executed
doesn't guarantee test has been initialized?
If this is the case how does one avoid problems with static
initialisation. For instance if I have a class A
class A
{
MyMutex mutex;
};
A a_object;
Here, let us say that MyMutex's constructor will read a static member
variable within the MyMutex class. Presumably, this is undefined
behavior since the static variable may not yet have been initialised.
If that is the case how do you avoid this sort of trap?