J
John Carson
One routinely hears that the order of initialisation of objects with static
storage duration in different translation units cannot in general be
guaranteed. I have a question about one way to influence the order of
initialisation.
Suppose that class A contains both a static object and a static member
function that refers to that static object in some way (by, e.g., printing
out its value or returning its address).
1. Am I right to believe that, in these circumstances, the calling of class
A's static member function is enough to ensure the prior initialisation of
class A's static member, even when the function call is made from another
translation unit?
2. Suppose that the constructor of another class, class B, calls class A's
static member function. If the answer to 1 is yes, would it not follow that
declaring a B object, even at namespace scope in another translation unit,
will ensure that class A's static object is initialised in time for the B
object's constructor call?
storage duration in different translation units cannot in general be
guaranteed. I have a question about one way to influence the order of
initialisation.
Suppose that class A contains both a static object and a static member
function that refers to that static object in some way (by, e.g., printing
out its value or returning its address).
1. Am I right to believe that, in these circumstances, the calling of class
A's static member function is enough to ensure the prior initialisation of
class A's static member, even when the function call is made from another
translation unit?
2. Suppose that the constructor of another class, class B, calls class A's
static member function. If the answer to 1 is yes, would it not follow that
declaring a B object, even at namespace scope in another translation unit,
will ensure that class A's static object is initialised in time for the B
object's constructor call?