cangrejo
cangrejo

Reputation: 2202

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>)'

Upvotes: 1

Views: 60

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

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.

Upvotes: 3

Related Questions