Static member variable initialization order

Discussion in 'C++' started by aakash, Apr 21, 2011.

  1. aakash

    aakash Guest

    Suppose in file a.hpp, we have a struct

    struct Base
    {
    static int x;
    }

    int Base::x = 1;

    and in file b.hpp, we have a struct

    struct Derived: Base
    {
    static int y;
    }

    int Derived::y = 2;

    Is it guaranteed that when we compile these two class file, static
    variable x will be initialized before static variable y?
    I know that initialization of static variables are not defined, but
    since x is in a base class, will it be first initialized?
     
    aakash, Apr 21, 2011
    #1
    1. Advertising

  2. On 4/21/2011 7:04 AM, aakash wrote:
    > Suppose in file a.hpp, we have a struct
    >
    > struct Base
    > {
    > static int x;
    > }
    >
    > int Base::x = 1;
    >
    > and in file b.hpp, we have a struct
    >
    > struct Derived: Base
    > {
    > static int y;
    > }
    >
    > int Derived::y = 2;
    >
    > Is it guaranteed that when we compile these two class file, static
    > variable x will be initialized before static variable y?


    No.

    > I know that initialization of static variables are not defined, but
    > since x is in a base class, will it be first initialized?


    No.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 21, 2011
    #2
    1. Advertising

  3. aakash

    aakash Guest

    On Apr 21, 5:55 pm, Victor Bazarov <> wrote:
    > On 4/21/2011 7:04 AM, aakash wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Suppose in file a.hpp, we have a struct

    >
    > > struct Base
    > > {
    > >      static int x;
    > > }

    >
    > > int Base::x = 1;

    >
    > > and in file b.hpp, we have a struct

    >
    > > struct Derived: Base
    > > {
    > >      static int y;
    > > }

    >
    > > int Derived::y = 2;

    >
    > > Is it guaranteed that when we compile these two class file, static
    > > variable x will be initialized before static variable y?

    >
    > No.
    >
    > > I know that initialization of static variables are not defined, but
    > > since x is in a base class, will it be first initialized?

    >
    > No.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Why isnt that static members of base class arent compulsorily
    initailized first?
    Isnt the base class a dependency on the derived class?
     
    aakash, Apr 23, 2011
    #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. Sam
    Replies:
    4
    Views:
    474
    The Directive
    Jan 13, 2004
  2. asdf
    Replies:
    11
    Views:
    577
    Frederick Gotham
    Oct 3, 2006
  3. Replies:
    10
    Views:
    1,126
    Victor Bazarov
    Mar 15, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. aaragon
    Replies:
    2
    Views:
    621
    James Kanze
    Nov 2, 2008
Loading...

Share This Page