如何为C ++函数中的结构分配默认值? [英] How can I assign a default value to a structure in a C++ function?

查看:162
本文介绍了如何为C ++函数中的结构分配默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构:

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)

但我似乎没有找到一种方法来为eyeposition分配默认值。如何在C ++中执行此操作?

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

推荐答案

这是

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 )

但有一个小技巧:仅为第1和第3个参数执行默认值。还有一件事:C是一种语言,C ++是另一种语言。

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.

这篇关于如何为C ++函数中的结构分配默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆