dimanche 1 mars 2020

static initialisation order issue with static/dynamic linking

If a c++ application is organised in following manner

//file1.cpp
static Y sgObj = X::getInitObject(0);  //declared in file scope

//file2.cpp
namespace{
   int getInitValue()
   {
      static int val = Y::sObj.initVal();
      return val;
   }
}

namespace X{
   Y getInitObject(int initVal)
   {
      if(initVal < getInitValue())
      {
         cout << "Print some log message" << endl;
      } 

      return Y::initMethod(initVal);
   }
}

//file.h
//This class is shipped as static and dynamic library, and application is supposed to link 
//them in their executable based on their need
class Y{
public:
int initVal();
static Y initMethod(int val);
static Y sObj;
};

Assuming all the necessary header files are included and we are able to create the executable for this application, does it have initialization issue with the way I am creating the object in file1.cpp and using:

  • static vs dynamic linking with the library exposed for creating the object.

Aucun commentaire:

Enregistrer un commentaire