Temporary Variables: Procrastination is the Thief of Time
C++ is a powerful programming language to learn, in part because it gives you full control over memory management. This is a two-edged sword, however; it lets you improve the performance of your code, but it also lets you shoot yourself in the foot. Therefore, it is important to understand the C++ compiler. This article examines how and why the compiler creates temporary objects, among other topics.
Temporary Variables: Procrastination is the Thief of Time - Visualizing the unseen object (Page 5 of 6 )
There is a simple way to find out how the compiler performs these tricks: just print the different stages it goes through when dealing with our object. Another way would be to step through the code with a debugger while the application is running, but there is nothing wrong with using print statements (Brian Kernighan prefers them over the use of a debugger still [Kernighan] :).
The constructor of ‘myObject’ and ‘foo’ was called as expected, but there is also an ‘unseen object’ being created when foo() returns. This is the temporary object (‘unseen object’) that is needed to make the data of the ‘fooobject’ survive beyond the scope of the function call.
The result data stored the temporary object is copied into ‘myObject’ using its assignment operator, after which the temporary is destroyed.
Two extra objects were created to provide one object with data! That is quite a lot; all we wanted was to have some data survive beyond the scope of the function call. Wouldn’t it be nice if we could get rid of that temporary object?
It is very simple to let data survive beyond the scope of the function call when that data already exists outside the function. Instead of returning an object by value, we can return a reference or pointer to it. These refer to the object by address, and returning/copying addresses is much cheaper.
We have to make a small adaptation to the example to make sure that the ‘foo’ object exists outside of the function scope. Nasty things would happen if we were to return a reference to a local object that is destructed before we can use the reference!
In the function we can make the object static to the function, and let the function return a reference to this static object, so we can circumvent this problem.
Ah good! We got rid of the temporary object, which is exactly what we wanted to do because we are returning the address (reference) to a static object that exists outside the function:
Note that the foo2 constructor is only called the first time you call the foo function. This is an additional benefit that objects static to a function offer you: they are only constructed the first time you call the function. The Meyers Singleton [Meyers] uses this fact to help you prevent a (possibly expensive) construction, because when the function is never called, the object will not be created.