【问题标题】:Inconsistent behaviour passing arguments in C++在 C++ 中传递参数的行为不一致
【发布时间】:2013-10-25 17:59:53
【问题描述】:

鉴于这两个函数声明:

void initialize(int p, std::vector<Vector3> &);
std::vector<Vector3> toNurbsCoords(std::vector<Vector3>);

为什么会这样

Nurbs nurbs;
std::vector<Vector3> pts = nurbs.toNurbsCoords(points);
nurbs.initialize(degree, pts);

虽然这会引发编译时错误?

Nurbs nurbs;    
nurbs.initialize(degree, nurbs.toNurbsCoords(points));
//error: no matching function for call to 'Nurbs::initialize(int&, std::vector<Vector3>)'

【问题讨论】:

标签: c++ compiler-errors arguments


【解决方案1】:

因为临时不能绑定到非const 引用。

nurbs.toNurbsCoords(points) 是临时的。在第一种情况下,您使用它初始化命名对象 - pts - 并传递它。在第二种情况下,您只需传递温度。

【讨论】:

  • 小修正:“临时不能绑定到非 const lvalue 引用。” (回想一下,临时可以绑定到非 const rvalue 引用。)
猜你喜欢
  • 2011-11-27
  • 2016-04-24
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多