Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are static C++ class members initialized?

There appears to be no easy answer to this, but are there any assumptions that can be safely made about when a static class field can be accessed?

EDIT: The only safe assumption seems to be that all statics are initialized before the program commences (call to main). So, as long as I don't reference statics from other static initialization code, I should have nothing to worry about?

like image 521
Tony the Pony Avatar asked Sep 14 '09 13:09

Tony the Pony


People also ask

How do you initialize static class members?

For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value. The following code will illustrate the of static member initializing technique.

How are static variables initialized in C?

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. If we change the program to following, then it works without any error.

Where should you initialize a static data member?

The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile .

How are static variables initialized?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.


1 Answers

The standard guarantees two things - that objects defined in the same translation unit (usually it means .cpp file) are initialized in order of their definitions (not declarations):

3.6.2

The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (basic.types) with static storage duration initialized with constant expressions (expr.const) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.

The other guaranteed thing is that initialization of static objects from a translation unit will be done before use of any object or function from this translation unit:

It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.

Nothing else i guaranteed (especially order of initialization of objects defined in different translation units is implementation defined).

EDIT As pointed in Suma's comment, it is also guaranteed that they are initialized before main is entered.

like image 190
Tadeusz Kopec for Ukraine Avatar answered Sep 28 '22 02:09

Tadeusz Kopec for Ukraine