August 2012
Intermediate to advanced
976 pages
30h 17m
English
Recall that operations on a reference are actually operations on the object to which the reference refers (§ 2.3.1, p. 50):
int n = 0, i = 42;int &r = n; // r is bound to n (i.e., r is another name for n)r = 42; // n is now 42r = i; // n now has the same value as ii = r; // i has the same value as n
Reference parameters exploit this behavior. They are often used to allow a function to change the value of one or more of its arguments.
As one example, we can rewrite our reset program from the previous section to take a reference instead of a pointer:
// ...
Read now
Unlock full access