Static class member initialization question

Discussion in 'C++' started by Michael Klatt, Nov 11, 2003.

  1. I am working on a library which uses the GKS graphics package. I have
    a Gks object which opens the GKS subsystem in its constructor:

    // Gks.hpp

    class Gks
    {
    public :
    Gks();
    };


    --------------------

    // Gks.cpp

    #include "Gks.hpp"
    Gks::Gks()
    {
    // open GKS subsystem
    }


    I have several types of object which need the GKS subsystem to be
    opened before they can be created. I need something like a global
    static Gks object, but since this is tricky to do in a library I'm
    trying a solution that uses inheritence. All classes which need a
    static Gks object derive from GksInit:

    // GksInit.hpp
    #include "Gks.hpp"

    class GksInit
    {
    public :
    virtual ~GksInit() {}

    protected :
    GksInit();

    private :
    static const Gks ourGks;
    };

    --------------------------

    //GksInit.cpp
    #include "GksInit.hpp"

    const Gks GksInit::eek:urGks; // make sure GKS gets initialized

    GksInit::GksInit() {}


    ----------------

    // Workstation.hpp
    // GKS needs to be initialized before a Workstation can be created
    #include "GksInit.hpp"

    class Workstation : public GksInit
    {
    ...
    };


    This all seems to work as desired, but here is my problem/question.
    If I inline the GksInit constructor in the header file, the
    GksInit::eek:urGks static member never gets constructed. Why is this?
    Michael Klatt, Nov 11, 2003
    #1
    1. Advertising

  2. Michael Klatt

    lilburne Guest

    Michael Klatt wrote:

    >
    > This all seems to work as desired, but here is my problem/question.
    > If I inline the GksInit constructor in the header file, the
    > GksInit::eek:urGks static member never gets constructed. Why is this?


    Static member variables are initialized before main is
    called. The inheritance GksInit and construction of a
    GksInit has nothing to do with it.

    Are you sure you haven't removed the

    'const Gks GksInit::eek:urGks;'

    declaration from the cpp file?
    lilburne, Nov 11, 2003
    #2
    1. Advertising

  3. lilburne wrote:
    >>
    >> This all seems to work as desired, but here is my problem/question.
    >> If I inline the GksInit constructor in the header file, the
    >> GksInit::eek:urGks static member never gets constructed. Why is this?

    >
    > Static member variables are initialized before main is
    > called. The inheritance GksInit and construction of a
    > GksInit has nothing to do with it.
    > ...


    No, dynamically initialized non-local objects are guaranteed to be
    initialized before the first use of any function defined in the same
    translation unit. This means that non-local objects, which are defined
    in the same translation unit with 'main', are guaranteed to be
    initialized before 'main' is called. But there's no such guarantee for
    non-local objects defined in other translation units.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 11, 2003
    #3
  4. Michael Klatt

    lilburne Guest

    Andrey Tarasevich wrote:

    > lilburne wrote:
    >
    >>>This all seems to work as desired, but here is my problem/question.
    >>>If I inline the GksInit constructor in the header file, the
    >>>GksInit::eek:urGks static member never gets constructed. Why is this?

    >>
    >>Static member variables are initialized before main is
    >>called. The inheritance GksInit and construction of a
    >>GksInit has nothing to do with it.
    >>...

    >
    >
    > No, dynamically initialized non-local objects are guaranteed to be
    > initialized before the first use of any function defined in the same
    > translation unit. This means that non-local objects, which are defined
    > in the same translation unit with 'main', are guaranteed to be
    > initialized before 'main' is called. But there's no such guarantee for
    > non-local objects defined in other translation units.
    >


    True you can't guarantee initialisation order across
    translation usits, but doesn't his GksInit::eek:urGks having
    static storage duration guarantee it being initialized
    before any dynamic initialization occurs?
    lilburne, Nov 11, 2003
    #4
  5. (Michael Klatt) wrote in message news:<>...
    > I am working on a library which uses the GKS graphics package. I have
    > a Gks object which opens the GKS subsystem in its constructor:
    >
    > // Gks.hpp
    >
    > class Gks
    > {
    > public :
    > Gks();
    > };
    >
    >
    > --------------------
    >
    > // Gks.cpp
    >
    > #include "Gks.hpp"
    > Gks::Gks()
    > {
    > // open GKS subsystem
    > }
    >
    >
    > I have several types of object which need the GKS subsystem to be
    > opened before they can be created. I need something like a global
    > static Gks object, but since this is tricky to do in a library I'm
    > trying a solution that uses inheritence. All classes which need a
    > static Gks object derive from GksInit:
    >
    > // GksInit.hpp
    > #include "Gks.hpp"
    >
    > class GksInit
    > {
    > public :
    > virtual ~GksInit() {}
    >
    > protected :
    > GksInit();
    >
    > private :
    > static const Gks ourGks;
    > };
    >
    > --------------------------
    >
    > //GksInit.cpp
    > #include "GksInit.hpp"
    >
    > const Gks GksInit::eek:urGks; // make sure GKS gets initialized
    >
    > GksInit::GksInit() {}
    >


    I just want to reiterate my question. The code I have posted here
    seems to work as I expect. However, if I inline the GksInit
    constructor in the header file, the constructor for static member
    ourGks never gets called. The only difference is the inlined
    constructor; the GksInit source file still contains the definition of
    ourGks.

    Why does it matter if the GksInit() is inlined or defined in the
    source file?
    Michael Klatt, Nov 12, 2003
    #5
  6. "Michael Klatt" <> wrote...
    > (Michael Klatt) wrote in message

    news:<>...
    > > I am working on a library which uses the GKS graphics package. I have
    > > a Gks object which opens the GKS subsystem in its constructor:
    > >
    > > // Gks.hpp
    > >
    > > class Gks
    > > {
    > > public :
    > > Gks();
    > > };
    > >
    > >
    > > --------------------
    > >
    > > // Gks.cpp
    > >
    > > #include "Gks.hpp"
    > > Gks::Gks()
    > > {
    > > // open GKS subsystem
    > > }
    > >
    > >
    > > I have several types of object which need the GKS subsystem to be
    > > opened before they can be created. I need something like a global
    > > static Gks object, but since this is tricky to do in a library I'm
    > > trying a solution that uses inheritence. All classes which need a
    > > static Gks object derive from GksInit:
    > >
    > > // GksInit.hpp
    > > #include "Gks.hpp"
    > >
    > > class GksInit
    > > {
    > > public :
    > > virtual ~GksInit() {}
    > >
    > > protected :
    > > GksInit();
    > >
    > > private :
    > > static const Gks ourGks;
    > > };
    > >
    > > --------------------------
    > >
    > > //GksInit.cpp
    > > #include "GksInit.hpp"
    > >
    > > const Gks GksInit::eek:urGks; // make sure GKS gets initialized
    > >
    > > GksInit::GksInit() {}
    > >

    >
    > I just want to reiterate my question. The code I have posted here
    > seems to work as I expect. However, if I inline the GksInit
    > constructor in the header file, the constructor for static member
    > ourGks never gets called. The only difference is the inlined
    > constructor; the GksInit source file still contains the definition of
    > ourGks.
    >
    > Why does it matter if the GksInit() is inlined or defined in the
    > source file?


    Sounds like a bug in the compiler. Perhaps it discards any consts
    if they are not used anywhere else in the system. See if using
    a different compiler would fix the problem.

    Victor
    Victor Bazarov, Nov 12, 2003
    #6
  7. I think you should make a function instead, it usually helps, not all
    compilers like statics floating around.

    Just do a:
    Gtk& GtkInstance(){
    static Gtk ourGtk;
    return ourGtk;
    }

    Jesper
    Jesper Madsen, Nov 18, 2003
    #7
    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. Siemel Naran
    Replies:
    4
    Views:
    790
    Micah Cowan
    Jan 12, 2005
  2. , India
    Replies:
    2
    Views:
    388
    Tadeusz B. Kopec
    Nov 13, 2007
  3. dolphin
    Replies:
    3
    Views:
    1,328
    Pete Becker
    Dec 5, 2007
  4. Replies:
    23
    Views:
    850
    James Kanze
    Jun 15, 2008
  5. Replies:
    4
    Views:
    326
    Joe Greer
    Jul 25, 2008
Loading...

Share This Page