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.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47

1 Answers1

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.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806