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


    Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer - Const-Correctness


    (Page 3 of 6 )

    When we pass a function argument by reference, the function is allowed to manipulate and make changes to the object being referenced. Some would even argue that for this very reason it is safer to pass function arguments by value when you want to make sure the function doesn’t make any alterations to them. I prefer to use constant references instead.

    By passing a const reference to a function, you forbid the function to make any changes to the object via that reference.  Now we can make our first alteration to the FindClosest function:

    Enemy const& FindClosest(list<Enemy> const &enemies,
         Player const &player) {
       assert (!enemies.empty()); 

          list<Enemy>::const_iterator closest;
          float min_sqdistance = FLT_MAX;
         
          list<Enemy>::const_iterator nmeIt;
          for (nmeIt=enemies.begin(); nmeIt!=enemies.end(); nmeIt++)
          {
           Vector3 nme2ply=player.GetPos() – nmeIt->GetPos();
           float sqdist=nme2ply.squaredLength();
           if (sqdist < min_sqdistance)
           {
            closest=nmeIt;
            min_sqdistance=sqdist;
           }
          }
    return *closest;
             }

    This alteration saves the need to construct an Enemy object local to the function body, and also the need to construct a temporary object to carry the result over to the caller.

    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

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT