Search code examples
c++default-argumentsmember-initialization

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.


Solution

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