Reputation: 19258
I believe I've come across part of the reason that so many people seem to be confused about const
which is that the term "const pointer" which seems to be somewhat counterintuitive nomenclature in that when said(even by those who understand the subtleties) seems often intended to mean "pointer-to-const" or:
const Type * foo;
However, when parsed without a priori knowledge of the convention (e.g. by newcomers),in the term "const pointer" one might parse that "const" qualifies "pointer" and so expect it to mean "const-pointer-to-type" or:
Type * const foo;
So my questions are:
const Type *
" despite seemingly being a misnomer?Type * const
" referred to in discussion?Upvotes: 3
Views: 219
Reputation: 146968
Yes- typically "const pointer" means pointer-to-const-object, not a-pointer-that-is-const, and it's probably because the first scenario is way more common, so it's understandable to assign to it the shorter form.
Normally for const-pointer-to-type
, you would either just say that, or stick with the designator: T* const
. It's very rare to discuss such pointers, so there is no particularly common expression for it as far as I know.
Upvotes: 3
Reputation: 3929
const is a so called qualifier. It qualifies that comes next.
In the case of const type* var ,it means that the value of type is const ,so it's called a const(ant) type pointer.
In the case of type* const var ,it means that the value of var is const ,so it's called a type pointer const(ant).
Notice the subtle difference between the two.
Upvotes: 1
Reputation:
Yes, const Type* foo
is usually called a const pointer.
The reason why this kind of constness has a name, is because this type of constness is more sticky than the other one. If you get a pointer-to-const object, you can not convert it to a non-const one. But if you get a pointer that itself is const, but points to a mutable object, you can alsways copy it to a writable field.
Lastly, pointer-to-const would suggest that the pointed object is const. It is not, it can change, you just have a read-only view of the object. In that sense, the constness applies more to the pointer than the object itself.
Upvotes: 3