< Previous | Contents | Next >
Understanding the Pitfalls of Reference
One way to efficiently give a function access to a large object is to pass it by reference. However, this introduces a potential problem. As you saw in the Swap program, it opens up an argument variable to being changed. But what if you
Passing References for Efficiency 197
don’t want to change the argument variable? Is there a way to take advantage of the efficiency of passing by reference while protecting an argument variable’s integrity? Yes, there is. The answer is to pass a constant reference.
Hin t
In general, you should avoid changing an argument variable. Try to write functions that send back new information to the calling code through a return value.