I am trying to learn C++ via some web tutorials. I don\'t have a compiler available to me, otherwise I would try this out. I\'m not sure what is meant by a const pointer. Doe
Your code is not legal. You can't assign to aPointer
(except using copy-style initialisation, which in fact is not assignment even though it looks like it) if aPointer
is declared const like that.
But usually when people say "a const pointer", they mean const int * aPointer
, not int * const aPointer
as you have in your code. The whole internet will explain the difference at the drop of a hat. As far as I know, the term "const pointer" isn't defined in the standard, so we're free to do this even though it's potentially confusing. "Pointer-to-const" would be an unambiguous description, and a pointer-to-const is much more commonly used than a pointer-which-is-itself-const.
A pointer-which-is-itself-const is used to refer to something, where you won't want the pointer to refer to a different thing at any point in its life. For instance, this
is a pointer-which-is-itself-const, because "this object" remains the same object through the execution of a member function. The C++ language has opted not to let you decide part way through that you want to assign some other value to this
, to make it refer to some other object. In C++ references often serve that purpose too, since they cannot be "re-seated" to change the referand.