why the c++ constructor was not called when it appear as the static member variable?

亡梦爱人 提交于 2019-12-01 06:54:27

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

No. Dynamic initialisation of an object with static storage duration is guaranteed to happen before execution of any function defined in the same translation unit. If there are no such functions, or your program never calls them, then there's no guarantee that it will ever be initialised.

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

An int variable is initialised statically, before the program starts, as long as its initialiser is constant.

is there any compile option will influence the variable ?

Not that I know of, but I'm not familiar with your platform. You could give yourself more control over the object's creation by scoping it within a function:

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

Now it is guaranteed to be initialised the first time the function is called. Of course, you'll need to remember to call it at some point.

The startup and shutdown of a C++ program are sort of grey areas because it's not clear how much of your code you can already use (because it has been initialized) and how much is yet starting. At shutdown the same happens for destructor... it's not clear how many subsystems have been already shut down when your static instances are destroyed.

Moreover you should never use static initialization for anything that may fail, debugging before the start or after the end of main can be very difficult.

Note also that the order in which statics are initialized is not defined (except relative to other statics in the same compilation unit) and it may change from one compilation to the next. This means that you may live happy with a working program until for some strange reason you get a different initialization order and things stop working without any relevant change in the code.

Using static initialization for extremely simple things is ok, for anything else is not and you should do instead proper controlled initialization.

I think there is a bug in your compiler.

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

#include <iostream>

using namespace std;

class A
{
    public:
        A() { cout << "Hallo" << endl; }
};

class B
{
    public:
    static A a;
};

A B::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 the static data member is constructed (commented line above).

Static initialization in C++ is:

  • Zero initialization
  • Constant initialization
  • Dynamic initialization

Hence your best bet is initialization at first function call:

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

EDIT:

If you want to initialize before main:

struct Initialize { 
    Initialize() { fn(); }
}

Initialize initialize;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!