My C++ compiler complains when i try to initialize a int member variable in class definition. It tells "only static const integral data members can be initialized within a class". Can you please explain the rationale behind this restriction (if possible with example).
-
Unless the integer that you speak of is common for all the objects of the type you are defining, what use do you have in a member variable being assigned a value? Calling that variable static const is the way to tell the compiler that the variable is common to every object of the type. – vpit3833 Dec 01 '10 at 09:46
-
I am trying to avoid initializing in constructor, giving a default value to the member variable. – Sulla Dec 01 '10 at 09:49
-
1See http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class and http://stackoverflow.com/questions/3575580/rationale-behind-static-const-member-initialization-syntax for some good info. – sje397 Dec 01 '10 at 10:06
5 Answers
Because it's not allowed in the current standard. According to Bjarne, you will be able to do this in C++0x. If you really need it, try setting the compiler to C++0x (-std=c++0x
in GCC) and see if your compiler supports it.

- 4,299
- 3
- 43
- 63
The rationale is the "low-level" nature of C++. If it would allow this, the compiler would need to generate initialization code for all constructors which is not entirely clear to the developer.
After all it might be necessary to initialize members of base classes on the construction of a derived class even when the base class constructors are not explicitly invoked.
Static const integral variables do not need intitalization upon object creation.

- 5,067
- 1
- 35
- 39
-
Because it remains the same ,the compiler can simply replace the constants with their values. – Tomas Dec 01 '10 at 09:56
-
See my answer - static implies that it's not per-instance, and as such is not related to the constructor. – EboMike Dec 01 '10 at 09:57
-
Static const integrals are initialized only once, and not each time an object of that class is constructed. – Cătălin Pitiș Dec 01 '10 at 09:57
The static restriction exists because C++ uses constructor initializers to initialize non-static data members:
struct Example {
int n;
Example() : n(42) {}
};
The const restriction exists because the const case is treated specially (rather than the other way around) so that static const integral members can usually be treated as if they had internal linkage, similar to const variables at namespace scope (C++03 §7.1.5.1p2, if you're interested). This is primarily beneficial to use the members in integral constant expressions, such as array sizes.

- 13,952
- 4
- 37
- 63
I'm just guessing you're trying to do this:
class foo {
int m_iX = 5;
};
This would require code to be run in the constructor, since every newly created instance would need to initialize this variable. In C++, all code that is run during the constructor is (luckily) contained in the constructor itself, so it is immediately obvious what the construction of the class entails. Furthermore, since a class can have any number of constructors (including copy constructors), it would be ambiguous as when this initialization should or should not take place.
You can do this:
class foo {
enum {
CONSTANT = 8
};
};
This allows you to use foo::CONSTANT
. That works since it will be per-class rather than per-instance.
Likewise, you can do this:
class foo {
static int sm_iX;
};
in the .cpp:
int foo::sm_ix = 5;
Again, this is per-class, not per-instance, and as such not relevant to the construction of an actual instance.
Bonus - if you declare this int const, many compilers might evaluate it at compile-time.

- 76,846
- 14
- 164
- 167
Arun,
I believe your question is related to
Compiler Error C2864
To achieve what you want to do, C++ requires you to initialize instance specific members (ie: non static, non cost) either in Constructor body or the initialization list.

- 1,665
- 1
- 19
- 36