intOne: 5
rSomeRef: 5
&intOne: 0x3500
&rSomeRef: 0x3500
OUTPUT
258 Day 9
Because the final two lines print memory addresses that might be unique to
your computer or to a specific run of the program, your output might differ.
CAUTION
Once again,rSomeRefis initialized as a reference to intOne. This time, the
addresses of the two variables are printed in lines 15 and 16, and they are
identical.
C++ gives you no way to access the address of the reference itself because it is not
meaningful as it would be if you were using a pointer or other variable. References are
initialized when created, and they always act as a synonym for their target, even when
the address-of operator is applied.
For example, if you have a class called President, you might declare an instance of that
class as follows:
President George_Washington;
You might then declare a reference to Presidentand initialize it with this object:
President &FatherOfOurCountry = George_Washington;
Only one Presidentexists; both identifiers refer to the same object of the same class.
Any action you take on FatherOfOurCountryis taken on George_Washingtonas well.
Be careful to distinguish between the &symbol on line 9 of Listing 9.2, which declares a
reference to an integer named rSomeRef, and the &symbols on lines 15 and 16, which
return the addresses of the integer variable intOneand the reference rSomeRef. The com-
piler knows how to distinguish these two uses by the context in which they are being
used.
ANALYSIS
Normally, when you use a reference, you do not use the address-of operator.
You simply use the reference as you would use the target variable.
NOTE