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.
Next: Examining possible dangers >>
More C++ Articles
More By J. Nakamura