![](/img/trans.png)
[英]How to return boost::fusion::vector<x,y,z> elements to add to a std::array<boost::fusion::vector<x,y,z>>?
[英]How to return a location as x,y,z
我怎样才能使返回的x和y而不只是其中之一? 在Location类中,我保护了名为x_pos
和y_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);
}
尽管我必须注意,此函数没有逻辑意义,但是您返回的是传递给它们的值,而无需修改它们。
你可能是说
void Location::getXY(double& x, double& y) const {
x = x_pos;
y = y_pos;
}
这样,您将Location
的内部x_pos
和y_pos
在提供的位置中。 您不需要从返回void
的函数return
任何东西(实际上,除了void
本身,您不能返回任何东西)。 正如大家之前所说,返回一个std::pair
可能是一个更好的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.