C++
  Home arrow C++ arrow Temporary Variables: Keep Your Values Clos...
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 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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: Keep Your Values Close, and Your References and Pointers Even Closer
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-09-26

    Table of Contents:
  • Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
  • To pass or not to pass by value
  • Const-Correctness
  • Call optimization
  • Examining possible dangers
  • The cost of implicit conversion

  • 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: Keep Your Values Close, and Your References and Pointers Even Closer


    (Page 1 of 6 )

    As you know, in programming C++, it is much better to return a reference to an object than it is to return that object by value. As we will see in this article, it is also much better to pass a function parameter by reference than it is to pass it by value. But there are exceptions to this, as Jun Nakamura explains.

    You will find it a very good practice always to verify your references, sir.

    [Martin Routh 1755-1854]

    Finding the Closest Enemy

    We have been looking at temporary objects and the effect they can have on the performance of an application. As an example I offered a function that can tell the AI of a game which enemy is located closest to the player from a list that was provided as a function argument.

    Refer to the previous article for the initial definition of the following function:

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

    The conclusion was that it is much better to return a reference to an object than it is to return that object by value. Likewise it is much better to pass a function parameter by reference than it is to pass it by value. We actually have to apply this rule to this function before we can return a reference to an enemy.

    More C++ Articles
    More By J. Nakamura


       · Hi, great article. Quick question:I have a function which takes a reference to...
       · Hi, this is one of those things that comes pretty intuitively, but looking it up in...
     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - 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







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