Order of initialisation of static objects in different translation units

Discussion in 'C++' started by John Carson, Jan 23, 2004.

  1. John Carson

    John Carson Guest

    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)
    John Carson, Jan 23, 2004
    #1
    1. Advertising

  2. Re: Order of initialisation of static objects in different translationunits

    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.
    Gianni Mariani, Jan 23, 2004
    #2
    1. Advertising

  3. John Carson

    John Carson Guest

    "Gianni Mariani" <> wrote in message
    news:burpas$
    > 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)
    John Carson, Jan 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton

    Tools for viewing 'translation units'?

    Steven T. Hatton, May 10, 2004, in forum: C++
    Replies:
    3
    Views:
    337
    Nils Petter Vaskinn
    May 10, 2004
  2. Steven T. Hatton
    Replies:
    7
    Views:
    2,328
    Howard
    Jun 3, 2004
  3. Victor Bazarov
    Replies:
    0
    Views:
    419
    Victor Bazarov
    May 10, 2006
  4. Mark A. Gibbs
    Replies:
    0
    Views:
    3,511
    Mark A. Gibbs
    May 10, 2006
  5. Tim Clacy
    Replies:
    8
    Views:
    382
    Tim Clacy
    May 30, 2006
Loading...

Share This Page