|
|
|
|
|
|
|
Enter a number
(020): 3
number: 3
square: 9
cubed: 27 |
|
|
|
|
|
|
|
|
Analysis: Listing 11.8 is identical to 11.7, with two exceptions. The ERR_CODE enumeration makes the error reporting a bit more explicit on lines 35 and 40, as well as the error handling on line 21. |
|
|
|
|
|
|
|
|
The larger change, however, is that Factor() is now declared to take references to squared and cubed rather than to pointers. This makes the manipulation of these parameters far simpler and easier to understand. |
|
|
|
|
|
|
|
|
In this hour, you learned what references are and how they compare to pointers. You saw that references must be initialized to refer to an existing object, and cannot be reassigned to refer to anything else. Any action taken on a reference is, in fact, taken on the reference's target object. Proof of this is that taking the address of a reference returns the address of the target. |
|
|
|
|
|
|
|
|
Q Why have references if pointers can do everything references can? |
|
|
|
|
|
|
|
|
A References are easier to use and understand. The indirection is hidden, and there is no need to repeatedly dereference the variable. |
|
|
|
|
|
|
|
|
Q Why have pointers if references are easier? |
|
|
|
|
|
|
|
|
A References cannot be null, and they cannot be reassigned. Pointers offer greater flexibility, but are slightly more difficult to use. |
|
|
|
|
|