Why does a non-constant static member have multiple definitions?

C ++ forces the programmer to define a non-constant static member outside the class, and the reason I continue to see is that if the static member was defined inside the class, this will lead to several definitions for the static member. I understand that a few definitions for a static member are bad, but I do not understand where these multiple definitions may arise. There should not be an initialized non-constant static member, just go to the data section and this will be the only definition?

struct Student { static int x = 4; // Why would this result in multiple definitions? }; 

In addition, I read in this other stackoverflow post that const const members are simply inserted into the code wherever it is used: Why don't I have a non-stationary static const member in the class? Is this handled by the preprocessor along with all the other directives? (I will ask about this in another post, if necessary, but I was not sure if he was worthy of a separate post).

+4
source share
1 answer

This will happen because / when your title is included in several "translation units" (think .cpp files).

Each TU will then contain a copy of the definition.

At connection time, they will collide. (Linker associates objects with each translation unit)

+6
source

All Articles