C++
  Home arrow C++ arrow Page 2 - Temporary Variables: Temporaries Are Not N...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Temporary Variables: Temporaries Are Not Necessarily Evil
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-10-03

    Table of Contents:
  • Temporary Variables: Temporaries Are Not Necessarily Evil
  • Binding a reference to a temporary object
  • Death by Returned const Reference
  • It can be Good to Return by Value

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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();
    crasher.shout();
    (void)printf(“Done!\n”);

    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();
    anotherPartyCrasher.shout();
    (void)printf(“Done!\n”);

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

    #include <stdio.h>
    class MyClass {
     char const *mName;
    public:
     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!’
    Done!

    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!’
    Done!
    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.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT