30

According to Static data members on the IBM C++ knowledge center:

The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.

Why is that? What's the schematic behind that regarding the memory allocation?

Mat
  • 202,337
  • 40
  • 393
  • 406
henryyao
  • 1,758
  • 6
  • 23
  • 37
  • Always wondered this myself. It always "seemed" like the header would be a more appropriate place for statics to be defined. – crush Sep 11 '13 at 18:55
  • 1
    @koodawg Well Java combines header + source implementation =] – crush Sep 11 '13 at 18:56
  • 7
    The reason is that a class might be declared (through #include) in many Translation Units, if it was defined on all of them there would be a repeated symbol. – imreal Sep 11 '13 at 18:56
  • 2
    Possible Duplicate: http://programmers.stackexchange.com/questions/145299/why-the-static-data-members-have-to-be-defined-outside-the-class-separately-in-c – Natan Streppel Sep 11 '13 at 18:56
  • Possible Duplicate: http://stackoverflow.com/questions/13662441/c11-allows-in-class-initialization-of-non-static-and-non-const-members-what-c – Bill Lynch Sep 11 '13 at 18:56
  • http://www.stroustrup.com/C++11FAQ.html#member-init – Mike Makuch Sep 11 '13 at 18:57
  • 1
    static class members have external linkage, and must comply to the restrictions placed on that model, one definition only. There are exceptions (`static const` integral members) as compilers have gotten smarter. If it helps at all, consider them like a global `extern` decl, where somewhere there has to be a definition to fulfill that bill. – WhozCraig Sep 11 '13 at 19:01
  • possible duplicate of [Why do non-const, non-int/enum static data members have to be initialized outside the definition?](http://stackoverflow.com/questions/15167254/why-do-non-const-non-int-enum-static-data-members-have-to-be-initialized-outsid) – UpAndAdam Sep 11 '13 at 19:17
  • Possible Duplicate: http://stackoverflow.com/questions/3536372/defining-static-members-in-c – UpAndAdam Sep 11 '13 at 19:27

4 Answers4

32

It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.

Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.

In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
12

It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. @Nick pointed this out as well.

From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class

A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.

Yihe
  • 4,094
  • 2
  • 19
  • 21
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
9

As of C++17 you can now define static data members inside a class. See cppreference:

A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition:

struct X {
     inline static int n = 1; 
};
user2108462
  • 855
  • 7
  • 23
0

According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.

As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.