Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I had a strange problem ,

declare a static member variable whose name is B class in A class . And initialize in the cpp file. but the constructor of B class was never called. I try to use some small test , the test constructor could be called normally. so it is very strange for our production system.

The 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 with

Test TsdNAExtPersonality::test;

I have tried in several way, but i found all of ways are unusefull.

  1. did not set the variable as member variable but as global variable ==> also can't output
  2. change the member variable as pointer and change the initialize way as using new ==> no

the environment is HP-UX ,and the compile is aCC

so my question are :

  1. is there any compile option will influence the variable ? in other words, all the static variable will not be initialized.

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

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

  4. is there any mistake in my code ?

like image 262
A New Star Avatar asked Sep 13 '13 16:09

A New Star


People also ask

Can static data members be used in a constructor?

1) Static variables are property of the class and not the object. 2) Any static variable is initialized before any objects is created. That's why compiler don't allow to define static variable inside constructor as constructor will be called when a object is created.

Can a static variable be initialized in a constructor?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.

Can a static variable be initialized in a constructor C++?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).

Why is static member variable not defined inside the class?

Because static member variables are not part of the individual class objects (they are treated similarly to global variables, and get initialized when the program starts), you must explicitly define the static member outside of the class, in the global scope.


2 Answers

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.

like image 56
Mike Seymour Avatar answered Sep 23 '22 10:09

Mike Seymour


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.

like image 44
6502 Avatar answered Sep 22 '22 10:09

6502