why you can not initialize the variable inside the class in c++

c++

Solution

Actually you can. But only in C++11.

The following is a valid C++11 code:

class A
{
    int x = 100; //valid in c++11
};

Your compiler might not support this, but GCC 4.8.0 compiles it fine.

Hope that helps.

Problem

i know that you can not initialize the member variable(other than static const) inside the class directly without using constructor. but just wanted to know what is the reason behind this. below is the code snippet if any body could help ``` class a { int c=5; // giving error error C2864: 'a::c' : only static const integral data members can be // initialized within a class int b; public: a():c(1),b(2){} void h() { printf("%d,%d",c,b); } }; int main() { a l; l.h(); getchar(); } ```

Original source

Related problems