Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member initialization lists with default arguments

Is it possible use default arguments with member initialization lists?

Vector3::Vector3(double xI, double yI, double zI)
: x(xI=0), y(yI=0), z(zI=0)
{
}

The constructor always sets x, y, and z to 0 even if you call it with setting the arguments.

like image 799
Daniel says Reinstate Monica Avatar asked Feb 17 '23 08:02

Daniel says Reinstate Monica


2 Answers

Vector3(double xI=0, double yI=0, double zI=0);  

Vector3::Vector3(double xI, double yI, double zI)
    : x(xI), y(yI), z(zI)
    {
    }

Also, if you are wondering what your code is doing, it's simply setting your parameters to be 0, then passing the value of them (now 0) to initialize the members.

like image 136
matt Avatar answered Feb 23 '23 18:02

matt


The assignment operator = always return what it has assigned to left side variable, in your case it return 0, which get assigned to x,y and z.

like image 41
AlexDan Avatar answered Feb 23 '23 18:02

AlexDan