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

76 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 answers

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