11

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!

Cail Demetri
  • 2,138
  • 3
  • 22
  • 24
  • 3
    Probably the same reason we can't say `SomeClass::someMemberFunction {...}` to define a member function. – chris Jul 17 '13 at 07:51
  • 1
    Come to think of it, it would lighten up the syntax a bit and prevent those pesky errors that arise from a typo or missing specifier, but in the case of functions, at least, it can be useful to put `const` on value parameters in the definition, but not declaration, though I could get used to doing the latter if this syntax was an option. – chris Jul 17 '13 at 07:55
  • It would complicate the grammar more. There is already a declaration/expression ambiguity, this would take it even further. – Angew is no longer proud of SO Jul 17 '13 at 07:58
  • 1
    Oh, and you'd have to consider overloads for functions. For non-overloaded functions, I can't see why it wouldn't *work*, but the overloads would make it more complex. Who knows, maybe that was the rationale behind functions not being given the treatment, and variables followed suit. It's always *possible* I guess. – chris Jul 17 '13 at 08:01

3 Answers3

7

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

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

class nameHere
{
    public:
        static const int totalNum = {0};
}
  • 3
    You can do that in C++03 too. I don't think there has been any change concerning static data member initialization in C++11. What is new is that you dan initialize non-static data members at the point of declaration. – juanchopanza Jul 17 '13 at 08:15
  • @juanchopanza, With `static`, you can now initialize non-integral types in there as long as it's a `constexpr`. – chris Jul 17 '13 at 08:47
2

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.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85