SunQuest
 
       C++
  Home arrow C++ arrow 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Temporary Variables: Temporaries Are Not Necessarily Evil


    (Page 1 of 4 )

    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. 

    A thing may look specious in theory and yet be ruinous in practice;

    A thing may look evil in theory, and yet be in practice excellent.

    [Edmund Burke 1729-1797]

    Finding the Closest Enemy

    In the first article I made my first attempt to write a function that could determine which enemy is located closest to the player for a hypothetical video game. That first attempt gave us the following function declaration:

    Enemy FindClosest(list<Enemy> enemies, Player player);

    Of course there was a lot of room for improvement, since these articles serve to make a point about temporary objects. References are much more efficient in use than objects passed by value (as long as you remain aware that a const reference can cause the compiler to create a temporary object when a function call can be completed by using a conversion constructor)… thus we ended with the following declaration:

    Enemy& FindClosest(list<Enemy> &enemies, Player const &player);

    We now know what problems the passing of objects by value may bring, but returning objects by value is not necessarily evil. If we forced ourselves to use references all the time, we would miss out on some of the nicer features of the C++ language. On top of that we’ve seen that it is dangerous to return const references!

    Another optimization could be found in the way the returned reference was handled. Returning a reference is useless when it is used to create a copy of the closest enemy regardless:

    Enemy closest=FindClosest (enemies, player);

    This will use an enemy object (stored in the enemies list) directly because we use the returned reference directly:

    Enemy &closest=FindClosest (enemies, player);

    What will happen in the last statement when FindClosest returns an object by value? This means that we are binding a reference to a temporary object. Is this safe?

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away
    - Temporary Variables: Temporaries Are Not Nec...
    - Temporary Variables: Keep Your Values Close,...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway