TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OLC#012Checked automatically with code checker

Synopsis:Do not initialise static variables with other (external) static variables
Language:C++
Severity Level:6
Category:Object Life Cycle


Description:
The standard states that static variables are zero initialised before their first use. The order of initialization is not defined across compilation units. So when a static variable is being initialized with another (external) static variable it might be initialised with only the zero initialised value instead of the programmer intended initialised value.

Violating example:

extern int i;
 
class A
{
public:
    // error: i might be initialised with 0 or the intended value
    static int j = i;
};


Non-violating example:

int i = 5;

class A
{
public:
    static int j = i;
};


Literature References:
Ellemtel Rec. 33