0

This is the example from Effective modern c++.

class Widget {
…
private:
int x{ 0 }; // fine, x's default value is 0
int y = 0; // also fine
int z(0); // error!
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
zk9099
  • 183
  • 10

1 Answers1

-1

direct initialization using ()

Inside the class treats the below
int z(0);

As a function as and expects parameter .As result error

Expected parameter declarator

alternatively can be done

class Widget {
private:
    int x{ 0 };
    int y = 0;
    int z;
public:
    Widget():z(0){}
};
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
  • This is wrong. `std::string s("");` doesn't work here either, and `int z(0);` is correct in other contexts – M.M Sep 10 '17 at 03:01