Question

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?

Was it helpful?

Solution

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;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top