lets say I have the functions
std::Vector<Point> calculate() {
std::Vector<Point> points; //do stuff with points
return points;
}
and
void calculate(std::Vector<Point>& points) {
//do stuff with points
}
So my question is specific to objects initialized on the stack, and are stl objects. Is there any difference in performance, and what is the popular method of doing it
regards
When you want to return one or more items with a data type then it is better to use an output parameter. Generally, use an output parameter for anything that needs to be returned. When you want to return only one item with only an integer data type then it is better to use a return value.
Returning the object should be used in most cases because of an optimsation called copy elision. However, depending on how your function is intended to be used, it may be better to pass the object by reference.
My humble opinion: return values create more problems than solve... they look simpler, they are more concise in the calling segment, but rvalue references would be avoided...
The shortcoming is that expressiveness will be reduced, but that is a small cost, since resources like memory allocation will move upstream.
C++ must and can be significantly simplified in terms of syntax and resource management.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With