Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign a default value to a structure in a C++ function?

I have a structure:

typedef struct {
   double x,y,z;
} XYZ;

I want to define a function like this:

double CalcDisparity(XYZ objposition, 
                     XYZ eyeposition, 
                     double InterOccularDistance = 65.0)

But I can't seem to find a way to assign a default value to eyeposition. How can I do this in C++?

like image 542
russellpierce Avatar asked Sep 10 '10 20:09

russellpierce


1 Answers

It's

struct XYZ{
    XYZ( double _x, double _y, double _z ) : x(_x), y(_y),z(_z){}
    XYZ() : x(0.0), y(42.0), z(0.0){}

    double x, y, z;
};

so that I now have a default constructor. Then you call it like this:

double CalcDisparity( XYZ objposition = XYZ(),
                      XYZ eyeposition = XYZ(),
                      double interOccularDistance = 65.0 )

But there's one small trick: you can't do a default value for the 1st and 3rd arguments only. One more thing: C is a language, C++ is another language.

like image 86
wheaties Avatar answered Nov 15 '22 01:11

wheaties