Summary

You will use references extensively when you build C++ classes, the subject of Part 2. As you do so, remember the following points about references:

A reference is an alias for an actual variable.

A reference must be initialized and cannot be changed.

References are most useful when passing user-defined data types to a function, and when returning values from a function.

Reference declarations are sometimes confused with the operation of taking the address of a variable, because both have the form &identifier. To distinguish between these two uses of &, remember the following rules:

When &identifier is preceded by the name of a type, such as int or char, the & means “reference to” the type. This occurs only in declarations, such as declaring the type of a reference variable, the type of a parameter, or a function's return type.

When &identifier is not preceded by the name of a type, the & means “address of” the variable. This occurs most commonly when passing an argument to a function or when assigning a value to a pointer.

Note that there is no difference between type &identifier and type& identifier; both syntaxes declare references.