Home arrow C++ arrow Page 5 - Temporary Variables: Procrastination is the Thief of Time

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.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
September 19, 2005
  1. · Temporary Variables: Procrastination is the Thief of Time
  2. · Temporary Object Terminology
  3. · More than just a call
  4. · To return or not to return an object
  5. · Visualizing the unseen object
  6. · Back to the closest Enemy

print this article

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] :).

#include <stdio.h>
class MyClass {
 char const *mName;
 MyClass(char const *name) : mName(name) {
  (void)printf(“%s – Constructor called.\n”, mName);

 MyClass(MyClass const &other) : mName(“unseen object”) {
 (void)printf(“%s – Copy Constructor called.\n”, mName);

~MyClass() {
 (void)printf(“%s – Destructor called.\n”, mName);

MyClass& operator=(MyClass const &rhs) {
 (void)printf(“%s – Assignment Op. called.\n”, mName);

MyClass foo() {
 MyClass obj(“foo”);
 return obj;

Now we can see what happens when we assign the result of foo to an object MyClass we have instantiated ourselves:

(void)printf(“\n-- test1 --\n”);
MyClass obj(“myObject”);
obj = foo();

Here is the description of the work done:

-- test1 --
myObject - Constructor called.
foo - Constructor called.
unseen object - Copy Constructor called.
foo - Destructor called.
myObject - Assignment Operator called.
unseen object - Destructor called.

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.

MyClass& foo2() {
 static MyClass obj(“foo2”);
 return obj;

Then we rerun our test:

(void)printf(“\n-- test2 --\n”);
MyClass obj(“myObject”);
obj = foo2();

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:

-- test2 --
myObject - Constructor called.
foo2 - Constructor called.
myObject - Assignment Operator called.

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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials