Why is the C ++ constructor not called when it is displayed as a static member variable?

I had a strange problem,

declares a static member variable whose name is class B in class A. And initialize the cpp file. but the constructor of class B was never called. I am trying to use a small test, the test constructor can be called normal. therefore, it is very strange for our production system.

Code like this in hpp :

class Test { public: Test() { ofstream file("/tmp/wup.txt",ios::app); file << "wup in test" << endl; file.close(); } }; //## An extended personality class TsdNAExtPersonality : public TsdNAPersonality{ public: TsdNAExtPersonality( s_gg62_personRec * gg62Header, TsdNAFunctionType requiredFunctionType); private: static Test test; public: TsdNAExtPersonality( string * personalityFile, TsdNAFunctionType requiredFunctionType); }; 

And in another cpp file, I initialize

 Test TsdNAExtPersonality::test; 

I tried several methods, but I found that all methods are not useful.

  • did not set the variable as a member variable, but since the global variable ==> also cannot output
  • change the member variable as a pointer and change the initialization method using the new ==> no

environment is HP-UX and compilation is CC

so my question is:

  • Is there any compilation parameter affecting the variable? in other words, the entire static variable will not be initialized.

  • from the C ++ standard, it should be called when the library was loading, right?

  • I set another static int value using the same path, it could be initialized. but the class constructor is not called, very strange.

  • Is there an error in my code?

+4
source share
4 answers

from the C ++ standard, it should be called when the library was loading, right?

Not. Dynamic object initialization with static storage duration is guaranteed until any function defined in the same translation unit is executed. If there are no such functions, or your program never calls them, then there is no guarantee that it will ever be initialized.

I set another static int value using the same path, it could be initialized. but the class constructor is not called, very strange.

The int variable is initialized statically before the program starts, if its initializer is constant.

Is there any compilation option for the variable?

Not that I know, but I am not familiar with your platform. You can give yourself more control over creating an object by viewing it inside a function:

 static Test & test() { static Test test; return test; } 

Now it will be initialized the first time the function is called. Of course, you will need to remember it at some point.

+4
source

Starting and ending a C ++ program is a gray area because it is not clear how much of your code you are already using (because it has been initialized) and how much has not yet begun. When shutting down, the same thing happens for the destructor ... it is not clear how many subsystems are already disabled when your static instances are destroyed.

Also, you should never use static initialization for anything that might fail, debugging before or after main can be very complicated.

Note also that the order in which the statics are initialized is not defined (except for relatively different statistics in the same compilation unit), and it can change from one compilation to the next. This means that you can be satisfied with the work program until, for some strange reason, you get a different initialization order and everything stops working without any code changes.

Using static initialization for extremely simple things is okay for something else, and you need to do the correct managed initialization.

+1
source

Static initialization in C ++:

  • Zero initialization
  • Continuous initialization
  • Dynamic initialization

Therefore, your best bet is to initialize the first time you call the function:

 int fn() { static int result = 42; return result; } 

EDIT

If you want to initialize before main:

 struct Initialize { Initialize() { fn(); } } Initialize initialize; 
0
source

I think there is an error in your compiler.

Running this simple code in linux / g ++ gives the expected results:

 #include <iostream> using namespace std; class A { public: A() { cout << "Hallo" << endl; } }; class B { public: static A a; }; AB::a; // < here the constructor must be called! int main() { cout << "Main runs" << endl; return 0; } 

Results in:

 Hallo Main runs 

The constructor MUST be called when a static data item is being constructed (commented out line above).

0
source

Source: https://habr.com/ru/post/1502085/


All Articles