Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I initialize static class variables in C++?

In C and C++ all static variables are initialized by default to ZERO.

This is not the case of static class data members. Why is that?

#include <iostream>
using namespace std;

int var;

class MyClass
{
public:
    static int classVar;
};
int MyClass::classVar = 0;  // Why I have to init it here?

int main(void)
{
    cout << ::var << endl;          // this is initalized to ZERO by default
    static int var;
    cout << var << endl;            // and this also is initalized to Zero
    cout << MyClass::classVar << endl;

    return 0;
}
like image 908
Muhammad Hewedy Avatar asked Nov 25 '11 10:11

Muhammad Hewedy


People also ask

Why do we need to initialize static variables?

You initialize a static object with a constant expression, or an expression that reduces to the address of a previously declared extern or static object, possibly modified by a constant expression.

Is it necessary to initialize static variables C?

It is mandatory to initialize the static variable using the static keyword in C else it will return an error. The static variable is only initialized the first time when a function is called. In a static variable, the memory of a static variable is allocated.

Why do we use static variables in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Do static classes need to be initialized?

A static constructor is called automatically. It initializes the class before the first instance is created or any static members declared in that class (not its base classes) are referenced.


2 Answers

At class scope,

int MyClass::classVar = 0;  // Why I have to init it here?

is a definition and

static int classVar;

is a declaration, ie. a promise the variable will be defined somewhere: you must define exactly once the variables you declare.

The rationale is that the class declaration will likely be included in multiple source files. Would a part of it be a definition, it would take place multiply: this is erroneous (exceptions are inline [member] functions).

Note that according to value initialization rules, you can get along with

int MyClass::classVar;  // Zero-initialized !

as a definition.

Variables declared at namespace scope are definitions too (unless they are extern qualified):

int var;

is a declaration, and a definition: if you put this into a header and include it in multiple translation units, you have an error ("multiply defined symbol", or something along those lines).

[Note that in C++ (and not in C), if the var above is const, it becomes automatically static and there is no violation of the One Definition Rule should it be put into a multiply included header. This goes slightly off topic, but feel free to ask details]

like image 198
Alexandre C. Avatar answered Nov 15 '22 05:11

Alexandre C.


C++ FAQ 10.12 states that:

static data members must be explicitly defined in exactly one compilation unit.

From C++ FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12

Does that answer your question or were you after a reference to the C++ standard itself?

like image 32
Talvalin Avatar answered Nov 15 '22 05:11

Talvalin