2

I am Java developer and I am new in C++. Try below code and it display initialization of non-const static member error.

class omtFirst {
int a = 8,b = 2;
public:
void printThis(string);
int addNumber(){        
    return (a+b);
};
};

After search on net i found that we can not initialize variable in the class description. But in Java it is possible. Why in C++ we can not initialize variable in the class description.

1 Answer 1

4

Because the standard says so, and the compiler is obliged to follow suit. There is no technical reason; in fact in C++11 it is possible to do this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.