C++ References
When we pass values to a function or return values from a function, that is exactly what we are doing. Passing/returning by value. What happens is that a copy of the value held by the variable is made, and sent into the function where it is used.
The significance of this is two-fold:
If we want the function to make a permanent change to a variable, this system is no good to us.
When a copy is made, to pass in as an argument or return from the function, processing power and memory are consumed. For a simple
int
or even perhaps a sprite, this is fairly insignificant. However, for a complex object, perhaps an entire game world (or background), the copying process will seriously affect our game's performance.
References are the solution to these two problems. A reference is a special type of variable. A reference refers to another variable. An example will be useful:
int numZombies = 100; int& rNumZombies = numZombies;
In the code above we declare and initialize a regular...