Why is it not allowed in C++ ?
Until and unless you define it, the variable doesn't become a l-value.
Why are const members allowed to be initialized ?
Even in this case, a definition is required if you are going to take the address of the variable.
9.4.2 Static data members
2 The declaration of a static data
member in its class definition is not
a definition and may be of an
incomplete type other than
cv-qualified void. The definition for
a static data member shall appear in a
namespace scope enclosing the member’s
class definition. In the definition at
namespace scope, the name of the
static data member shall be qualified
by its class name using the ::
operator. The initializer expression
in the definition of a static data
member is in the scope of its class
Also, this is primarily an usage artifact so that you can write:
class S {
static const int size = 42;
float array[ size ];
};
Does this mean static variables in C++ are not initialized with 0 as
in C?
No they are:
3.6.2 Initialization of non-local variables
Variables with static storage duration
(3.7.1) or thread storage duration
(3.7.2) shall be zeroinitialized (8.5)
before any other initialization takes
place.
Though things get a bit more trickier in C++0x. All literal types can now be initialized (as opposed to only integral types in the current standard) which would mean that all scalar types (floats included) and some class types can now be initialized using an initializer in the declaration.