Home arrow C++ arrow Page 3 - Temporary Variables: Procrastination is the Thief of Time

Temporary Variables: Procrastination is the Thief of Time

C++ is a powerful programming language to learn, in part because it gives you full control over memory management. This is a two-edged sword, however; it lets you improve the performance of your code, but it also lets you shoot yourself in the foot. Therefore, it is important to understand the C++ compiler. This article examines how and why the compiler creates temporary objects, among other topics.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
September 19, 2005
  1. · Temporary Variables: Procrastination is the Thief of Time
  2. · Temporary Object Terminology
  3. · More than just a call
  4. · To return or not to return an object
  5. · Visualizing the unseen object
  6. · Back to the closest Enemy

print this article

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)

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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials