Before C++17 you needed to define and/or initialise a non-const class member object separately from the class definition, otherwise you got a linker error:
//MyClass.h
extern int globalInt;
class MyClass
{public:
static int classStaticMemberInt;
};
//MyClass.cpp
int globalInt = 7;
int MyClass::classStaticMemberInt = globalInt;
Choosing this method I know that the MyClass::classStaticMemberInt object is initialised after the "int globalInt". It's defined only once, and I choose where to define it.
After C++17 we have the choice of avoiding writing everything twice, and just doing:
//MyClass.h
extern int globalInt;
class MyClass
{public:
static inline classStaticMemberInt = globalInt;
}
But if I include the "MyClass.h" header in a few compilation units, and I want, for example, to be sure that "int globalInt" is initialised before int MyClass::classStaticMemberInt, how can I know? I imagine the static inline in-class syntax in C++17 essentially tells the compiler to define that variable/object once, "somewhere", but where is that somewhere? For example if I do the following:
// Some cpp
int globalInt = 7; // Global object definition
#include MyClass.h // Include class MyClass,
// which static inline defines classStaticMemberInt
//to be equal to "int globalInt"
Is the class member object guaranteed to have the right value because the preprocessor pastes the class definition after "int globalInt = 7;"?
Also, let's just say that I paste the "MyClass.h" header into another .cpp, because the compiler should 'define' that object only once in one translation unit does it mean that it can choose to define it in the .cpp that I didn't define the global object in? And therefore any care I take to paste the header after the global int definition is pointless?