Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is initialization of static member of a class guaranteed before initialization of a static object of that class?

Tags:

c++

static

I encountered this C++ static initialization order related query at a code review recently.

  1. I've a class with static member variable in a compilation unit
  2. I've a static object of that class using a constructor in a different compilation unit

Here, I want to know if the static member variable is guaranteed to be initialized before the static object constructor is called?

MyClass.h:

typedef int (*MyFunc)(int);
class MyClass {
MyClass(MyFunc fptr) {
    mFunc = fptr;
}
static MyFunc mFunc;
}

MyClass.cpp:

MyFunc MyClass::mFunc = nullptr;

MyDifferentClass.h:

MyDifferentClass {
public:
    static int MyStaticFunc(int);
}

MyDifferentClass.cpp:

static MyClass myClassObj(MyDifferentClass::MyStaticFunc);

In the code, would mFunc be initialized to nullptr before myClassObj gets created? The reason for the query is that if the order is not guaranteed, then mFunc may get initialized again to nullptr.

like image 928
iVoid Avatar asked Aug 09 '17 15:08

iVoid


People also ask

Are static variables initialized before main?

Global (namespace) variables or static class members 1 live for the entire execution of the program: they must be initialized before main() is run and destroyed after execution finishes.

How do static members of a class get initialized?

A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

Is it mandatory to initialize static data members outside the class?

The reason for this is simple, static members are only declared in a class declaration, not defined. They must be explicitly defined outside the class using the scope resolution operator. If we try to access static member 'a' without an explicit definition of it, we will get a compilation error.

Is it mandatory to initialize static variable?

Initialization of Instance variables But if you declare an instance variable static and final Java compiler will not initialize it in the default constructor therefore, it is mandatory to initialize static and final variables. If you don't a compile time error is generated.


1 Answers

In the code, would mFunc be initialized to nullptr before myClassObj gets created? The reason for the query is that if the order is not guaranteed, then mFunc may get initialized again to nullptr.

The answer to the question is "Yes".

Setting aside the issue of initialization of thread specific objects, initialization of non-local variables is carried out in the following order.

  1. All variables are zero-initialized (order not specified). This is called zero initialization.
  2. All variables that can be initialized using constant values are initialized. This is called constant initialization.

Those ( 1 and 2 above) are called static initialization.

After that, dynamic initialization is performed.

In your case, MyClass::mFunc is initialized using constant initialization while myClassObj is initialized using dynamic initialization. Hence, the former is guaranteed to be initialized first.

More on this topic can be found at https://timsong-cpp.github.io/cppwp/n3337/basic.start.init.

like image 116
R Sahu Avatar answered Oct 25 '22 13:10

R Sahu