Return by Value
Another path that leads to temporary object creation is function return value. If you code a function that returns an object by value (as opposed to a reference or pointer), you can easily end up with a temporary. Consider f() as a simple example:
string f() { string s; ... // Compute "s" return s; }
The return value of f() is an object of type string. A temporary is generated to hold that return value. For example:
String p; ... p = f();
The temporary object holding f()'s return value is then assigned to the left-hand side object p. For a more concrete example consider the string operator+. This operator will implement the intuitive interpretation of string "+" operation. It takes two input string objects and returns a new ...
Get Efficient C++ Performance Programming Techniques now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.