Alex Rivera | Logout

Defining static members in C++

Asked 2010-08-21T04:49:39.827
34

I am trying to define a public static variable like this :

public :
         static int j=0;        //or any other value too

I am getting a compilation error on this very line : ISO C++ forbids in-class initialization of non-const static member `j'.

  1. Why is it not allowed in C++ ?

  2. Why are const members allowed to be initialized ?

  3. Does this mean static variables in C++ are not initialized with 0 as in C?

Thanks !

Edit
Report

1 Answer

17

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.

answered 2010-08-21T05:41:23.493

Your Answer