gpt4 book ai didi

c++ - 如何将位置返回为 x、y、z

转载 作者:行者123 更新时间:2023-11-28 05:42:43 25 4
gpt4 key购买 nike

我怎样才能返回 x 和 y,而不是其中一个?在我的 Location 类中,我保护了名为 x_posy_pos 的数据。

void Location::getXY(double& x, double& y) const {
x_pos = x;
y_pos = y;
return x, y;
}

最佳答案

你可以返回一个std::pair。我假设您想按值返回,而不是在此处按引用返回。

#include <utility> // for std::pair 

// if using c++14, else use std::pair<double,double> as return type
auto Location::getXY(double& x, double& y) const {
x_pos = x;
y_pos = y;
return std::make_pair(x,y);
}

虽然我必须指出这个函数没有逻辑意义,但您返回的是您在开始时传递的值而没有修改它们。

关于c++ - 如何将位置返回为 x、y、z,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com