1

Possible Duplicate:
Initializing private static members
Why I can't initialize non-const static member or static array in class?

It is strange to me. Why not assume there is a static field at the global scope?

2
  • 4
    Relevant. Does that answer your question?
    – dirkgently
    Commented Jun 15, 2012 at 6:55
  • Because it still uses a separate compilation model, when modules kick in (like in other languages), then this will become a non-issue. Commented Jun 15, 2012 at 7:16

1 Answer 1

5

It has to be placed somewhere (in some object file), so linker could find it. If you have declaration of class with static filed in .h file and include this file in a few .cpp files, then it would be ambiguous, which object file should have place allocated for this filed.

Please also note, that primitive type const static field could be initialized in class declaration:

class Foo
{
    static const int n = 42;
};
3
  • Actually it is only const integral types and const enums that can be initialized in the class declaration. Commented Jun 15, 2012 at 7:23
  • @juanchopanza: That is true only for C++03 and not C++11. Check my answer to the marked duplicate(the second marked duplicate)
    – Alok Save
    Commented Jun 15, 2012 at 19:22
  • @Als maybe it is because it is Friday night, but I have read and re-read 9.4 and I just don't see it. Commented Jun 15, 2012 at 19:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.