How to initialize a non-constant static member of a class before creating an instance of a class in the main function ?

For example:

 class A { protected: static int x; A(){} public: }; class B: public A { }; int main() { // нужно инициализировать x здесь B b; } 
  • Initialization necessarily in main you want to see? You can just do it earlier through int A::x = A::initX(42); (borrowed the name from the @ixSci response). - αλεχολυτ
  • @alexolut yes, the value 42 calculated directly in main ; the main thing is that the essence is clear - swaq

2 answers 2

Add a static function that will assign the value of x and call it:

 class A { protected: static int _x; A() {} public: static void InitX(int x) { _x = x; } }; 

In main :

 int main() { A::InitX(5); B b; } 

If initialization is needed, you cannot control when it will be executed, but it will definitely be executed before main .

In your code, by the way, there is just not enough initialization, and without it the code will not gather. Add the following to the global scope:

 int A::x = 0; 
  • Those. it is necessary to initialize, no matter what the value will then be assigned using a static function? - swaq
  • @swaq, this is called a definition, it is not necessary to give an initializer ( = 0 ), but the definition is necessary. If you omit the initializer, the int object will be automatically zeroed out. - ixSci
  • Will an object of its own type be filled with garbage? - swaq
  • The @swaq for the custom type will call the constructor (either written by hand or provided by the implementation). The presence or absence of garbage depends on the data members of the class. - αλεχολυτ
  • @swaq, garbage will not be in any case, because the static memory area is reset, then objects are already built on the zeros (if there are constructors) - ixSci

Static class fields are always initialized before calling the main function.

This is due to the fact that static fields, in their essence, are the same global variables, only with a name in a special format. Accordingly, they are stored in the same place and in the same way - being “sewn” in the data section of the executable file.

We found out when the value of static fields is given. We now turn to how to do it.

We cannot specify the value of the field in the constructor of the owning class — the static field is not only not a single instance of the class, it is also known until the start of the start function. We also cannot set the value in the class declaration itself, since the field is stored in one place, and it is accessed from many places; Who among the applicants will be responsible for initializing the memory cell with a variable? Note: at least it was before C ++ 14, but how it is implemented there is a separate question .

Therefore, both initialization and reservation of a static class field is performed similarly to those for global variables (another point of their similarity), namely, by mentioning it in some .cpp file. Like that:

 int A::_x; // Значение поля будет определено позднее 

Or so:

 int A::_x = 12; // Значение поля «вшито» в исполняемый файл 

Or even like this:

 // myStaticFunction — статический метод класса A. // Можно уверенно утверждать, что все указанные в нём // действия выполняются строго до вызова функции main. int A::_x = A::myStaticFunction(); 

That is, the field type is indicated first, then its full name with all namespaces, and, finally, what this field will be initialized with - a constant (more precisely, a literal), the return value of the function or nothing at all.

Let us dwell on the second method, as the most consistent with the requirements of the author of the question. Initialization can be done not just by any function (except for non-static methods for obvious reasons), but in general by any functional object.

However, there is one significant limitation. According to the standard, the order of initialization of static fields is reliably known only within (simplifying) one .cpp file. As a result, by the time our function is called, only those fields that are specified in exactly the same way, but higher in the same file, are reliably initialized. The order of initialization of fields and variables from other .cpp files depends on the compiler and the order of listing the files with source codes in the project. All this should be taken into account when accessing other static fields and global variables from a function.