Home arrow C++ arrow Page 2 - Temporary Variables: Temporaries Are Not Necessarily Evil

Temporary Variables: Temporaries Are Not Necessarily Evil

In earlier articles, we learned about the problems the passing of objects by value can cause in C++. Returning objects by value, on the other hand, is not necessarily evil. Jun Nakamura explains the finer points.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
October 03, 2005
  1. · Temporary Variables: Temporaries Are Not Necessarily Evil
  2. · Binding a reference to a temporary object
  3. · Death by Returned const Reference
  4. · It can be Good to Return by Value

print this article

Temporary Variables: Temporaries Are Not Necessarily Evil - Binding a reference to a temporary object
(Page 2 of 4 )

It is clear that the lifetime of a temporary object is exactly that… temporary. For the same reason, it is not possible to return a pointer or reference to a local object -- because its lifetime is temporary. So beware and stay clear of the following type of code:

MyClass& partyCrasher(void) {
 (void)printf(“party crasher called!\n”);
 MyClass localCrasher(“partyCrasher”);
 return localCrasher;

MyClass &crasher=partyCrasher();

But what if the returned object is a temporary variable?

MyClass anotherPartyCrasher(void) {
 (void)printf(“another party crasher called!\n”);
 return MyClass(“anotherPartyCrasher”);

MyClass &crasher=anotherPartyCrasher();

Lets examine what the compiler is generating by reusing the MyClass class from the first article:

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

 MyClass(MyClass const &other) : mName(“unseen gatecrasher”) {
  (void)printf(“%s copied from %s\n”, mName, other.mName);

 ~MyClass() {
   (void)printf(“%s destroyed.\n”);

 void shout() const {
  (void)printf(“%s shouts:‘Crash the party!’\n”, mName);

First we try the partyCrasher function:

party crasher called!
partyCrasher created.
partyCrasher destroyed.
¿³? shouts:’Crash the party!’

Hopefully your compiler has generated a warning about returning the address of a local variable or temporary in this function. It is wise to heed your compiler’s warnings, as this shows that only a fool would ignore them.

The output makes it clear that the partyCrasher was destroyed before it was used. This is all as we expected.

Here is what happens when we try anotherPartyCrasher function:

another party crasher called!
anotherPartyCrasher created.
AnotherPartyCrasher shouts:’Crash the party!’
AnotherPartyCrasher destroyed.

Lo and behold! Apparently the temporary object is only destroyed when it leaves the scope of the function where it was created for an expression that needed it. This behavior is actually guaranteed by the C++ Standard, so it is fine to bind a reference to a temporary object as long as you only use it in the same scope it was created in.

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