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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.