< Previous | Contents | Next >
In this chapter, you should have learned the following concepts:
n A reference is an alias; it’s another name for a variable.
n You create a reference using &—the referencing operator.
n A reference must be initialized when it’s defined.
n A reference can’t be changed to refer to a different variable.
n Whatever you do to a reference is done to the variable to which the reference refers.
n When you assign a reference to a variable, you create a new copy of the referenced value.
218 Chapter 6 n References: Tic-Tac-Toe
n When you pass a variable to a function by value, you pass a copy of the variable to the function.
n When you pass a variable to a function by reference, you pass access to the variable.
n Passing by reference can be more efficient than passing by value, especially when you are passing large objects.
n Passing a reference provides direct access to the argument variable passed to a function. As a result, the function can make changes to the argument variable.
n A constant reference can’t be used to change the value to which it refers.
You declare a constant reference by using the keyword const.
n You can’t assign a constant reference or a constant value to a non-constant reference.
n Passing a constant reference to a function protects the argument variable from being changed by that function.
n Changing the value of an argument variable passed to a function can lead to confusion, so game programmers consider passing a constant reference before passing a non-constant reference.
n Returning a reference can be more efficient than returning a copy of a value, especially when you are returning large objects.
n You can return a constant reference to an object so the object can’t be changed through the returned reference.
n A basic technique of game AI is to have the computer consider all of its legal moves and all of its opponent’s legal replies before deciding which move to take next.