Using the const qualifier with references
The const
qualifier can be used to qualify the data in which references are initialized or refer to. We can also use const
qualified references as arguments to functions and as return values from functions.
It is important to understand that a reference is implemented as a constant pointer in C++. That is, the address contained within the reference variable is a fixed address. This explains why a reference variable must be initialized to the object to which it will refer, and may not later be updated using an assignment. This also explains why constant qualifying the reference itself (and not just the data that it refers to) does not make sense. This variety of const
qualification is already implied with its underlying implementation.
Let’s take a look at these various scenarios using const
with references.
Using references to constant objects
The const
qualifier can be used to indicate that the data to which references...