In general, it is a good idea to always declare
variables as constant if they doesn’t need
to be modified, because this makes sure that
they won’t be changed anywhere in the program
by mistake which in turn will help to prevent
bugs.
A variable in C++ can be made into a constant
by adding the const keyword either before
or after the data type. This modifier means
that the variable becomes read-only and it
must therefore be assigned a value at the
same time as it is declared. Attempting to
change the value will now result in a compile-time
error.
When it comes to pointers const can be used
in two ways. First, the pointer can be made
constant, which means that it cannot be changed
to point to another location. And second,
we can declare that the pointee is constant.
This means that the variable pointed to cannot
be modified through this pointer. It is also
possible to declare both the pointer and the
pointee as constant to make them both read-only.
Note that constant variables may not be pointed
to by a pointer to a non-constant type. This
prevents us from accidentally rewriting a
constant variable using a pointer.
References can also be declared as constant
in the same way as pointers. However, since
reseating a reference is never allowed declaring
the reference as const would be redundant.
It only makes sense to protect the referee
from change.
Just as with variables, pointers, and references,
objects can also be declared as constant.
This not only prevents us from accidentally
reassigning the object to another instance.
It also prevents us from changing the fields
that the object contains.
Because of this restriction a constant object
may not call a non-constant method since these
methods are allowed to change the object’s
fields. They may only call constant functions,
which are methods that are marked with the
const modifier just before the method body.
This const modifier means that the method
is not allowed to modify the state of the
object and can therefore safely be called
by a constant object of the class. More specifically,
the const modifier applies to the “this”
pointer that is implicitly passed to the function.
This effectively restricts the method from
modifying the object’s fields or calling
any non-constant methods in the class. Note
that in addition to making the method constant
the return type and method parameters may
also be made read-only. For example, if we
wanted to return a field by reference instead
of by value from a constant function we must
make sure that it is returned as a constant
in order to maintain the constness of the
object. Note that not all C++ compilers will
be able to catch this subtle mistake.
Static and instance fields in a class can
also be declared as constant. A constant non-static
field must be assigned its value using the
constructor’s initialization list. This
is the same as the preferred way of initializing
non-constant non-static fields. A constant
static field on the other hand must be defined
outside of the class declaration, the same
way as with non-constant static fields. The
exception to this is if the constant static
field is of an integer data type. Such a field
may also be initialized within the class at
the same time as the field is declared.
