Constness

A variable can be declared as constant which disallows it's initial value to be changed after the declaration:

const double pi = 4 * atan(1);

The type qualifier const, combined with any other type, modifies the type of the variable in a way that disallows any assignment expressions on that variable. An initializer (such as an assignment operator followed by an initial value) should always be used after declaring a variable as constant.

A constant variable is useful if the particular value it stores will never change and would otherwise be cumbersome to express as a literal for reasons such as: a high usage frequency, the literal is too lengthy, the literal is too difficult to remember. Now a non-constant variable satisfies all these needs, but it is good practice to make some things explicit and to not require more functionality out of a variable or object than what you really need.

Even though a type such as const int is technically different from int, they are still compatible in one direction such that you can assign a value of type const int to a variable of type int; no conversion happens.

In fact, the type of any literal is a constant of that type. For example, the literal 123 is more specifically of type const int rather than just int. This makes sense because one cannot take a literal and place it on the left-hand side of an assignment operator.

References

Another kind of entity that must be initialized upon declaration is a reference.

A reference does not store a value like a variable does, instead it refers to an existing variable. Observe the following code:

int x = 4; int& r = x; r = 5; cout << x << endl; x = 6; cout << r << endl;

As one can see, the variable x and the reference r are associated with the same storage and they become indistinguishable from each other after declaration.

From the whole code, it is known that x stores an integer value and r is said to be a "reference to" x, the type of r is called "reference to int". All uses of r after reference declaration evaluate the same as if x was used instead.

One can also declare a reference as a reference to a constant:

int x = 0; const int& r = x;

In which case, the reference can only be evaluated but not assigned to. It can still be said that r is a "reference to constant int" even though the object it refers to is not actually constant, but through this reference it is constant.

A reference must be initialized with a variable; after which that variable is said to be "bound" to that reference for the duration of that reference's lifetime. References cannot be "rebound" to another variable after declaration. A reference does not necessarily occupy storage and thus does not store a value, a reference always refers to an existing region of storage (such as a variable); therefore a reference cannot be called a variable in the strictest sense.

In the next page, you will see why references are useful. They are useful for sharing data across groups of code without the unnecessary copying of data.