O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required