C++
  Home arrow C++ arrow Page 3 - Temporary Variables: Procrastination is th...
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: Procrastination is the Thief of Time
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2005-09-19

    Table of Contents:
  • Temporary Variables: Procrastination is the Thief of Time
  • Temporary Object Terminology
  • More than just a call
  • To return or not to return an object
  • Visualizing the unseen object
  • Back to the closest Enemy

  • 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: Procrastination is the Thief of Time - More than just a call


    (Page 3 of 6 )

    Presume you are working on the AI for a video game where there are multiple enemies on the screen that have to attack the player in an intelligent way. The enemies are stored in a list, and we have to create a function that can determine which enemy is closest to the player.

    Here is a first (naďve) attempt:

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

    Enemy closest;
    float min_sqdistance = FLT_MAX;

    list<Enemy>::iterator nmeIt;
    for (nmIt=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;
    }

    Can you spot all of the temporary objects used in this function? Before you point out that closest, min_sqdistance, nmeIt, nme2ply, and sqdist are all temporary objects, be aware that a compiler doesn’t see them this way. For a compiler these are variables that are local to a function and a ‘for’ loop, therefore they are not temporary at all. Note that they also fit the definition of an lvalue.

    Temporary objects do not appear in your code, but are created by the compiler behind the scenes. This is why they are also called unnamed objects (or rvalues). There are two reasons why a compiler might have to do this:

    1. Because a function returns an object.
    2. Because an implicit conversion must be applied to make a function call succeed.

    More C++ Articles
    More By J. Nakamura


     

    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 2 hosted by Hostway
    Stay green...Green IT