Constants
syntax:
Constant Formal Parameters
When we use call by reference, the actual variables are used in the function.
Those variables can also be altered in the function.
To avoid this problem, we can declare the formal parameters as const, so that the parameters will stay constant during the execution of function and its value will not be changed.
Constant Methods
If a member function of a class is not allowed to change the data members of class, then we can use const keyword after the function name.
Constants and Pointer
Pointer to const
The pointer itself is not const → it can point to another address
The value pointed to is treated as read-only through this pointer
a itself is not const. You just promised the compiler you won’t modify it via this pointer.
Pointer to const variable
This is the same type as above.
The pointed object happens to be const.
Const pointer to a variable
The pointer is const, not the data.
The pointer always points to the same address
The value at that address can be modified
Const pointer to const data
We can neither change the address to which the pointer is pointing at, nor change the value at that address.