Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Variable initialisation for Classes in C++, why include data type?

I've been learning C++, and I've come across static variable (I have prior knowledge from C89), and in the resource i'm using, they've declared a static variable in a class such as:

class nameHere
{
    public:
        static int totalNum;
}

int nameHere::totalNum = 0;

int main()
{}

For Example. What I don't understand is that, since I've already declared that the static variable is an integer in the class definition, why do I need to also declare it as an integer outside of the class definition? Would it not make sense to simply initialise it like so:

nameHere::totalNum = 0;

int main()
{}

Is there a particular reason or simply a convention of C++? Thanks for all the help!

like image 468
Cail Demetri Avatar asked Jul 17 '13 07:07

Cail Demetri


People also ask

Why do we need to initialize static variables?

A static object of class type will use the default constructor if you do not initialize it. Automatic and register variables that are not initialized will have undefined values.

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.

Can we initialize static variable in class?

We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

What are static data members for a class and how they are declared and initialized?

Static data members are class members that are declared using static keywords. A static member has certain special characteristics. These are: Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.


3 Answers

This would (probably) make the language even more difficult to parse (and it's already almost insanely difficult to parse anyway).

As it is, the datatype (int, long, my_class, whatever) tells the compiler that what it's seeing is the beginning of a declaration (which, in this case, is also a definition). Without that, the compiler would have a rather more difficult time sorting things out.

In the specific case of things at global scope, it wouldn't be that bad, because at global scope about all you can have is a series of declarations. At any other scope, however, things would be more difficult (and having one rule at global scope, and another elsewhere would be ugly indeed).

like image 102
Jerry Coffin Avatar answered Sep 28 '22 05:09

Jerry Coffin


In C++11 you can simply initialize the variable inside the class:

class nameHere
{
    public:
        static const int totalNum = {0};
}
like image 43
Pascalau Razvan Avatar answered Sep 28 '22 05:09

Pascalau Razvan


There is a difference between a definition and a declaration. While the static variable in the class has been declared, it has not been defined. The One Definition Rule, explains declarations and definitions and states

In any translation unit, a template, type, function, or object can have no more than one definition. Some of these can have any number of declarations.

Therefore, the full type of the object must be used when declaring the variable.

like image 35
TheDarkKnight Avatar answered Sep 28 '22 04:09

TheDarkKnight