See also post on extern…
These rules are mostly based on [[c++primer]], about static Field, not local statics or file-scope static variables.
Rule 1 (the “Once” rule) — init must appear AND execute exactly once for each static field.
In my Ticker Plant xtap experience, the static field definition crucially sets aside storage for the static field. The initial value is often a dummy value.
Corollary: avoid doing init in header files, which is often included multiple times. See exception below.
Rule 2 (the “Twice” rule) — static field Must (See exception below) be DECLARED in the class definition block, and also DEFINED outside. Therefore, the same variable is “specified” exactly twice . However, the run time would “see” the declaration multiple times if it’s included in multiple places.
Corollary: always illegal to init a static field at both declaration and definition.
 Note ‘static’ keyword should be at declaration not definition. Ditto for static methods. See P117 [[essential c++]]
The Exception — static integer constant Fields are special, and can be initialized in 2 ways
* at declaration. You don’t define it again.
* at definition, outside the class. In this case, declaration would NOT initialize — Rule 1
The exception is specifically for static integer constant field:
- if non-const, then you can only initialize it in ctor
- if non-integer static, then you need to define it outside
- if non-const static, then ditto
- if not a field, then a different set of rules apply.
Rule 3: For all other static fields, init MUST be at-definition, outside the class body.
Therefore, it’s simpler to follow Rule 3 for all static fields including integer constants, though other people’s code are beyond my control.
——Here’s an email I sent about the Exception —–
It turned these are namespace variables, not member variables.
Re: removing “const” from static member variables like EXCHANGE_ID_L1
I believe you need to define such a variable in the *.C file as soon as you remove the “const” keyword.
I just read online that “integer const” static member variables are special — they can be initialized at declaration, in the header file. All other static member variables must be declared in header and then defined in the *.C file.
Since you will overwrite those EXCHANE_ID_* member variables, they are no longer const, and they need to be defined in Parser.C.