I have this class X
, using delegating constructor I want to change only the value i
and j
as 0. Is it possible to do so?
class X{
public:
X() = default;
X(int ival, int jval) : i{ ival }, j{ jval } {}
X(int new_i) : i{ new_i }, X(i, 0) {} //error here
private:
int i;
int j;
};
int main()
{
X x{ 345, 54 };
x = X{ 34 }; //annoymous copy changes only the i:member
return 0;
}
EDIT: I know X(int new_int) : X{new_int, 0}
will work but i wanted to know what's the error if initialise one more variable in the list.
May be I have another z
and I want to initialise that along with i
and j
.
i.e. X(int new_i) :z{ new_i }, X(new_i, 0) {}