Static member variable initialization order


A

aakash

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?
 
Ad

Advertisements

V

Victor Bazarov

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
 
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top