iterator and const pointer
On page 108 in C++ primer Fifth edition by Lippman et al., it says:
A const_iterator
behaves like a const
pointer (§ 2.4.2, p. 62). Like a const
pointer, a const_iterator
may read but not write the element it denotes; [...]
I understand the function of const_iterator
, but is this comparison correct? I think its behavior is more like "pointer to const
".
Did I misunderstand something?
You are right. The authors explicitly refer to section 2.4.2 of their book (I have just updated your question to reflect this), where they define how they intend to use the terminology "const pointer" vs. "pointer to const". Given this precise definition, they should have said "pointer to const" in the section you quoted from.
(However, other authors in other contexts use the terminology less precisely, and outside this context, I would not find it unusual if somebody referred to "pointer to const" as "const pointer".)
When the author said "const pointer" I think he indeed meant "pointer to const".
The reason he probably does it is twofold:
We almost never use a constant pointer (at least not directly - templates don't count).
A pointer to const is written as const T*
, which you pronounce as "const pointer".
下一篇: 迭代器和const指针