C++
  Home arrow C++ arrow Page 4 - 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  
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: 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


    Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer - Call optimization


    (Page 4 of 6 )

    Now let’s take a better look at how we’d use this function:

    Enemy activeEnemy = FindClosest(enemyList, player);

    The first and simplest optimization that comes to mind is the application of a copy constructor. With the statement above, an object must be created by calling its constructor (first call), after which an object is assigned to it by calling its assignment operator (second call). If a copy constructor is used, only one call to that copy constructor has to be made:

    Enemy activeEnemy( FindClosest(enemyList, player) );

    Compilers can optimize this statement by replacing the call to the constructor and assignment operator with that of the copy constructor… but there are no guarantees.

    Instead of copying the data of the returned enemy into another Enemy object, we could use the const reference the function is returning:

    Enemy const &activeEnemy = FindClosest(enemyList, player);

    The obvious downside of a const reference is that we cannot make changes to the referenced object, and cannot use any of its functions unless they are declared const.

    If we need to change the state of an enemy, we will have to return a non-const reference. To be able to return a non-const reference, we’ll have to pass a non-const reference to the enemy list to the function as well. The reference to the player object can remain const since we don’t expect a function that works with enemies to make changes to the player object.

    Finally, here is a function declaration that is usable:

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

    The enemy list is no longer const, so we can replace the const_iterators in the function body with regular iterators again.

    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

    - 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 4 Hosted by Hostway
    Stay green...Green IT