August 2012
Intermediate to advanced
976 pages
30h 17m
English
constAs with any other object, we can bind a reference to an object of a const type. To do so we use a reference to const, which is a reference that refers to a const type. Unlike an ordinary reference, a reference to const cannot be used to change the object to which the reference is bound:
const int ci = 1024;const int &r1 = ci; // ok: both reference and underlying object are constr1 = 42; // error: r1 is a reference to constint &r2 = ci; // error: non const reference to a const object
Because we cannot assign directly to ci, we also should not be able to use a reference to change ci. Therefore, ...
Read now
Unlock full access