In C++ Primer 5th Edition 2.4.1 Reference to const
, there's a code example that I can't quite understand:
int i = 42;
const int &r1 = i; // we can bind a const int& to a plain int object
const int &r2 = 42; // ok: r1 is a reference to const
const int &r3 = r1 * 2; // ok: r3 is a reference to const
int &r4 = r * 2; // error: r4 is a plain, nonconst reference
On the 3rd line, isn't reference supposed to refer to a variable not a number?
If that's the case, why can r2
refer to 42
? I also can't understand the comment on the 3rd line.