6.2.2. Passing Arguments by Reference
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:
// ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.