I was trying to initialize non-static int
member variable of class in c++11. I encountered this in Scott Meyers book (9th release) at page 50. The book says error but doesn't say why. May be I am not able to read between the lines. Is there reason why this fails. I just wanted to know the reason behind the fail.
#include <iostream>
class A {
public:
A(){};
private:
int x = 1; // fine
int y{3}; // fine
int z(0); // error
};
int main() {
int x(5); // this works as well
A a;
std::cout << x << std::endl;
}
Aucun commentaire:
Enregistrer un commentaire