Order of initialisation of static objects in different translation units

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?
 
G

Gianni Mariani

John said:
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?
No.


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?

Usually, the way to guarentee construction is to provide a static member
function with a "static veriable in that static member function".

i.e.

struct A
{
static A & GetInstance()
{
static A a; // initialized first time called
return &a;
}
// stuff
};


calling A::GetInstance() will guarentee that that single instance of A
is initialized whenever it is first needed.
 
J

John Carson

Gianni Mariani said:
Usually, the way to guarentee construction is to provide a static
member function with a "static veriable in that static member
function".

i.e.

struct A
{
static A & GetInstance()
{
static A a; // initialized first time called
return &a;
}
// stuff
};


calling A::GetInstance() will guarentee that that single instance of A
is initialized whenever it is first needed.

Thanks for the reply.

I am familiar with the technique you describe but was curious as to whether
an alternative technique would work. Apparently not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top