Reputation: 639
I used CLion as an IDE, it reports an error in IDE as
field z must be initialized
It can compile and run. But if I change const int z{3};
to const int z=3;
, no error will be reported in IDE. My question is whether it is indeed an error of my codes or it is just a bug in the IDE? Any difference between these two initialization approaches? Did your IDE report this error?
#include <iostream>
using namespace std;
class Test
{
private:
const int x = 3;
int y;
const int z{3};
public:
Test(int);
int gety(){
return y;
}
};
Test::Test(int a){
y=x+4;
}
int main()
{
Test test(5);
std::cout << test.gety() << std::endl;
return 0;
}
Upvotes: 3
Views: 142
Reputation: 238351
whether it is indeed an error of my codes
There is no error in the code, it is OK.
or it is just a bug in the IDE?
It is a bug in whatever generates the error message. The IDE is high on my list of suspects but it could be another tool whose message the IDE relays.
Any difference between these two initialization approaches?
In this context (default member initializer) both syntaxes are semantically equivalent. There is no difference.
Upvotes: 6