|
|
|
|
|
|
|
According to Figure 17-1, there is only one built-in type remaining: the reference type. Like pointer variables, reference variables contain the addresses of other variables. The statement |
|
|
|
|
|
|
|
|
declares that intRef is a variable that can contain the address of an int variable. Here is the syntax template for declaring reference variables: |
|
|
|
|
|
|
|
|
Although reference variables and pointer variables both contain addresses of data objects, there are two fundamental differences. First, the dereferencing and address-of operators (* and &) are not used with reference variables. After a reference variable has been declared, the compiler invisibly dereferences every single appearance of that reference variable. This difference is illustrated below: |
|
|
|
|
|
|
|
|
|
Using a reference variable |
|
|
|
| | | | | | | | | | | | | | | | | |
|
|
|
|
|
|
Some programmers like to think of a reference variable as an alias for another variable. In the preceding code, we can think of intRef as an alias for gamma. After intRef is initialized in its declaration, everything we do to intRef is actually happening to gamma. |
|
|
|
|
|