I am a beginner to C++ and have the following question regarding the purpose of pointers where neither the address nor the value that it is pointing to can be changed (constEverything
in the example).
Example:
int main()
{
int i = 1;
int j = 2;
int* const constPoint = &i;
*constPoint = 3; // legal
constPoint = &j; // illegal
const int* constVal = &i;
*constVal = 3; // illegal
constVal = &j; // legal
const int* const constEverything = &i;
*constEverything = 3; // illegal
constEverything = &j; // illegal
}
In the example there are different types of pointers. With constVal
you can change the address and with constPoint
you can change the underlying value. For constEverything
you can do neither.
To me the purpose of such a pointer would be to pass things by constant reference. Why should I not just use const type &val instead? To me a const reference seems a lot easier and it makes const type* const val obsolete.