简体   繁体   中英

Inconsistent behaviour passing arguments in C++

Given these two function declarations:

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

why does this work

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

while this throws a compile time error?

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

Because a temporary can't bind to a non- const reference.

nurbs.toNurbsCoords(points) is a temporary. In the first case you initialize named object - pts - with it and pass that. In the second case, you just pass the temp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM