Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order of initialisation of static objects in different translation units

Reply
Thread Tools

Order of initialisation of static objects in different translation units

 
 
John Carson
Guest
Posts: n/a
 
      01-23-2004
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?


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-23-2004
John Carson wrote:
> 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.

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      01-23-2004
"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:burpas$(E-Mail Removed)
> John Carson wrote:
> > 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.


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.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialisation orders of global/namespace/class static objects. Taras_96 C++ 7 10-27-2009 12:23 AM
Initialisation of reference vs. initialisation of reference member Tim Clacy C++ 8 05-30-2006 06:14 PM
std::cout and static initialisation order clilley C++ 6 11-18-2004 07:56 PM
Tools for viewing 'translation units'? Steven T. Hatton C++ 3 05-10-2004 01:41 PM
CRT, Member Initialisation, static data, constant data, global objects Tim C++ 2 12-15-2003 07:15 PM



Advertisments