4

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.

Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57

2 Answers2

7
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.

matt
  • 4,042
  • 5
  • 32
  • 50
1

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.

AlexDan
  • 3,203
  • 7
  • 30
  • 46