|
Read it backwards ...
int* -Pointer to int
int const * -Pointer to const INT
int * const -Const pointer to int
int const * const -const pointer to const INT
Now the first const can is on either side of the type so:
const int * ==int const *
const int * const ==int const * const
If you want to go really crazy you can do things like this:
int ** -pointer to pointer to int
int ** const -A const pointer to a pointer to an int
int * const * -A pointer to a const pointer to an int
int const ** -A pointer to a pointer to a const INT
int * const * const -A const pointer to a const pointer to an int
- ...
And to make sure we is clear on the meaning of const const Int* Foo;int *const Bar //note, you actually need to set the pointer / /here because you can ' t change it later;)
foo is a variable pointer to a constant int. This is lets you a change, and the value of that. Most often this is seen with cstrings where you have a pointer to a const char . You could change which string of your point to if you can ' t change the content of these strings. This was important when the string itself was in the data segment of a program and shouldn ' t be changed.
bar is a const or fixed pointer to a value that can changed. This is a reference without the extra syntactic sugar. Because of this fact, usually your would use a reference where you would use a T* const pointer unless you need to allow null Pointers.
|
Distinguishing between const pointers