Re: Static members and initialization don't seem to work properly?

Discussion in 'C++' started by Victor Bazarov, Jul 31, 2003.

  1. "red floyd" <> wrote...
    > I'm trying to do something that's pretty weird, and I may be violating
    > the language, so I'd appreciate a sanity check. [...]
    >
    > Any clues or help?


    What you're running into is the unspecified order of static
    object creation and initialisation across translation units.

    The simplest way to overcome this is not to use static data
    declared an a namespace level, but instead use a reference
    or a pointer to static data in some auxiliary function:

    FactoryBlah& getFactoryBlah()
    {
    static FactoryBlah factory;
    return factory;
    }

    ....
    getFactoryBlah().doTheRegisteringBusiness();

    This ensures that before you call the member function, the
    object of the class for which you are calling it is constructed
    (static inside the 'get' function). BTW, in this case the
    member function is not static.

    Essentially, this is one of the implementations of the Singleton
    pattern. Compared with the one you used, it's just a bit more
    robust and doesn't depend on the order of static data creation.

    Victor
     
    Victor Bazarov, Jul 31, 2003
    #1
    1. Advertising

  2. Victor Bazarov

    red floyd Guest

    Victor Bazarov wrote:
    > "red floyd" <> wrote...
    >
    >>I'm trying to do something that's pretty weird, and I may be violating
    >>the language, so I'd appreciate a sanity check. [...]
    >>
    >>Any clues or help?

    >
    >
    > What you're running into is the unspecified order of static
    > object creation and initialisation across translation units.
    >
    > The simplest way to overcome this is not to use static data
    > declared an a namespace level, but instead use a reference
    > or a pointer to static data in some auxiliary function:
    >
    > FactoryBlah& getFactoryBlah()
    > {
    > static FactoryBlah factory;
    > return factory;
    > }
    >
    > ...
    > getFactoryBlah().doTheRegisteringBusiness();
    >
    > This ensures that before you call the member function, the
    > object of the class for which you are calling it is constructed
    > (static inside the 'get' function). BTW, in this case the
    > member function is not static.
    >
    > Essentially, this is one of the implementations of the Singleton
    > pattern. Compared with the one you used, it's just a bit more
    > robust and doesn't depend on the order of static data creation.
    >
    > Victor
    >
    >


    Victor,

    Thanks for the tip... It seems that I'm also running into a bug in
    VC.NET where it doesn't properly handle static data construction in a
    static library. In some cases, it doesn't seem to call the constructor
    at all!

    I had a sample that worked when it was all one source project, but when
    split up into a library and a main program, the constructors for the
    static data weren't being called properly. I'll double check it with
    GCC to make sure it's a VC-ism.
     
    red floyd, Aug 1, 2003
    #2
    1. Advertising

  3. Victor Bazarov

    red floyd Guest

    red floyd wrote:

    > Victor,
    >
    > Thanks for the tip... It seems that I'm also running into a bug in
    > VC.NET where it doesn't properly handle static data construction in a
    > static library. In some cases, it doesn't seem to call the constructor
    > at all!
    >
    > I had a sample that worked when it was all one source project, but when
    > split up into a library and a main program, the constructors for the
    > static data weren't being called properly. I'll double check it with
    > GCC to make sure it's a VC-ism.
    >


    Bummer... GCC (under Cygwin for WinXP) (mis)behaves in an identical
    fashion. Looks like it's time to rearchitect...
     
    red floyd, Aug 1, 2003
    #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. JFCM
    Replies:
    4
    Views:
    5,788
  2. balasubramaniam
    Replies:
    2
    Views:
    447
    Andrey Tarasevich
    Nov 30, 2003
  3. DanielBradley
    Replies:
    3
    Views:
    14,991
    Dave Moore
    Jun 2, 2004
  4. Stuart Brockman
    Replies:
    3
    Views:
    329
    Bo Persson
    Oct 23, 2008
  5. Lambuz
    Replies:
    0
    Views:
    755
    Lambuz
    Mar 18, 2009
Loading...

Share This Page