mercredi 28 décembre 2016

Different betwee {} and = when C++11 initialize variables

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;
}

Aucun commentaire:

Enregistrer un commentaire