Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
As you know, in programming C++, it is much better to return a reference to an object than it is to return that object by value. As we will see in this article, it is also much better to pass a function parameter by reference than it is to pass it by value. But there are exceptions to this, as Jun Nakamura explains.
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:
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.