Home:ALL Converter>Static Data Member Initialization

Static Data Member Initialization

Ask Time:2012-07-03T04:24:41         Author:CyberGuy

Json Formatter

Why must static data member initialization be outside the class?

class X
{
public:
      int normalValue = 5; //NSDMI
      static int i;
};

int X::i = 0;

Why is the static data member (here "i") only a declaration, not a definition?

Author:CyberGuy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11300652/static-data-member-initialization
Jonathan Wakely :

It's important to distinguish the initializer which says what its initial value is, and the definition. This modified code is valid, with the initializer in the class definition:\n\nclass X\n{\npublic:\n int normalValue = 5;\n static const int i = 0; // declaration, with initializer\n};\n\nconst int X::i; // definition\n\n\ni.e. What must be outside the class is a definition, not the initialization.\n\nThat's because a variable must have an address in memory (unless it's only used in limited situations, such as in compile-time constant expressions.)\n\nA non-static member variable exists inside the object it is a member of, so its address depends on the address of the object that contains it. Every time you create a new X you also create a new X::normalValue variable. The non-static data member's lifetime begins with the class' constructor. NSDMI syntax doesn't have anything to do with the variable's address in memory, it just allows you to provide an initial value in one place, instead of repeating it in every constructor with an explicit constructor initializer list.\n\nOn the other hand, a static member variable is not contained within an instance of the class, it exists independently of any single instance and exists from the start of the program, at a fixed address. In order for a static member variable (or any other global object) to get a unique address the linker must see exactly one definition of the static variable, in exactly one object file, and assign it an address.\n\nBecause a static variable needs exactly one definition in exactly one object file, it doesn't make sense to allow that definition to be provided in the class, since class definitions typically exist in header files and are included in multiple object files. So although you can provide an initializer in the class, you still need to define the static data member somewhere.\n\nYou can also look at it like declaring an extern variable:\n\nnamespace X {\n extern int i;\n}\n\n\nThis declares the variable, but there must be a definition somewhere in the program:\n\nint X::i = 0;\n",
2012-07-02T21:16:26
AnT stands with Russia :

You need to supply a separate definition for a static data member (if its odr-used, as defined in C++11) simply because that definition shall reside somewhere - in one and only one translation unit. Static class data members are basically global objects (global variables) declared in class scope. The compiler wants you to choose a specific translation unit that will hold the actual \"body\" of each global object. It is you who has to decide which translation unit to place the actual object to.",
2012-07-02T20:43:50
Viktor Latypov :

\"static\" class member is like a globally allocated variable (it is not related to the single class instance), so it must reside in some object file (and to be declared in the \".cpp\" file) as a symbol just like any global variable.\n\nSimple class member (non-static) resides in the memory block allocated for the class instance.",
2012-07-02T20:32:58
Jesse Good :

The simple reason is because classes are usually declared in header files, which often are included in multiple cpp files. Static data members have external linkage and must be declared in exactly one translation unit which makes them unfit for being defined inside a class.\n\nAs juanchopanza points out the following is allowed:\n\nstruct A\n{\n const static int i = 1;\n};\n\n\nHowever, this is only a declaration not a definition. You still need to define it if you are going to use i's address somewhere.\nFor example:\n\nf(int);\ng(int&);\n\nX<A::i> x; // Okay without definition for template arguments\nchar a[A::i]; // Okay without definition, just using value as constant expression\n&A::i; // Need a definition because I'm taking the address\nf(A::i); // Okay without definition for pass by value\ng(A::i); // Need a definition with pass by reference\n",
2012-07-02T20:50:44
juanchopanza :

Bear in mind that is is possible to initialize the static data member at the point of declaration if it is of const integral type of const enumeration type:\n\nFrom the C++03 standard, §9.4.2\n\n\n If a static data member is of const integral or const enumeration type, its declaration in the class\n definition can specify a constant-initializer which shall be an integral constant expression (5.19)\n\n\nstruct Foo {\n static const int j = 42; // OK\n};\n",
2012-07-02T20:53:30
Gigi :

When the compiler generate binary code from a unit (extreme simplification: a cpp file and all its included headers) it will emit a symbol for the static variable and eventually initialization code for that variable.\n\nIt is okay for a static variable symbol to be declared in multiple units, but it is not okay for it to be initialized multiple times.\n\nSo you must make sure that the initialization code is only emitted for a single unit.\nThis mean that the static variable must be defined in exactly one unit.",
2012-07-02T20:41:28
yy