Const_iterator and const pointer

On page 108 of the C ++ primer Fifth Edition from Lippman et al., He says:

A const_iterator behaves like a const pointer (§ 2.4.2, p. 62). Like the const pointer, a const_iterator can read but not write the element that it denotes; [...]

I understand the const_iterator function, but is this comparison true? I think its behavior is more like a "pointer to const ".

I didn’t understand something?

+7
c ++ iterator pointers
source share
2 answers

You're right. The authors explicitly refer to section 2.4.2 of their book (I just updated your question to reflect this), where they determine how they intend to use the terminology “const pointer” versus “pointer to const”. Given this exact definition, they should have indicated a "pointer to const" in the section that you referred to.

(However, other authors in other contexts use the terminology less accurately, and outside this context, I would not find it unusual if someone referred to a "pointer to const" as a "pointer to a constant.")

+3
source share

When the author said "const pointer", I think he really meant a "pointer to const".

The reason he probably does this is twofold:

  • We almost never use a constant pointer (at least not directly - templates are not taken into account).

  • A pointer to const is written as const T* , which you pronounce as a "constant pointer".

+1
source share

All Articles