Using the const and constexpr qualifiers
In this section, we will add the const
and constexpr
qualifiers to variables, and discuss how they can be added to functions in both their input parameters and as return values. These qualifiers will be used quite liberally as we move forward in the C++ language. The use of const
and constexpr
can enable values to be initialized, yet never again modified. Functions can advertise that they will not modify their input parameters, or that their return value may only be captured (but not modified) by using const
or constexpr
. These qualifiers help make C++ a more secure language. Let’s take a look at const
and constexpr
in action.
const and constexpr variables
A const
qualified variable is a variable that must be initialized, and may never be assigned a new value. It is seemingly a paradox to pair the usage of const
and a variable together – const
implies not to change, yet the concept of a variable is to inherently hold different...