- Home
- C# And C++
- Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
- By Samee Jhor
- Published 01/2/2007
- C# And C++
- Unrated
Samee Jhor
View all articles by Samee Jhor
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.
Spread The Word
Article Series
-
Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer

