why default initialization using parentheses is not permissible in c++?

96 Views Asked by At

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

There are 1 best solutions below

1
On

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