From 75c9dd3be16bbeb37d97cc8e758953dcf2655f58 Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Fri, 24 Aug 2001 15:28:20 +0000 Subject: [PATCH] added not about constness of operator* and operator[] [SVN r10931] --- iterator_adaptors.htm | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/iterator_adaptors.htm b/iterator_adaptors.htm index 8f172e2..62376e7 100644 --- a/iterator_adaptors.htm +++ b/iterator_adaptors.htm @@ -767,10 +767,10 @@ struct iterator_adaptor iterator_adaptor( const iterator_adaptor<B,Policies,V,R,P,Category,Distance>&); - reference operator*() const; + reference operator*() const; [6] operator_arrow_result_type operator->() const; [3] - value_type operator[](difference_type n) const; [4] + value_type operator[](difference_type n) const; [4], [6] iterator_adaptor& operator++(); iterator_adaptor& operator++(int); @@ -857,6 +857,34 @@ bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, Forward Iterator, so you will need to use a less restrictive iterator category such as std::input_iterator_tag. +

[6] + There is a common misconception that an iterator should have two + versions of operator* and of operator[], one + version that is a const member function and one version + that is non-const. Perhaps the source of this + misconception is that containers typically have const and + non-const versions of many of their member functions. Iterators, + however, are different. A particular iterator type can be either + mutable or constant (but not both). One can assign + to and change the object pointed to by a mutable iterator whereas a + constant iterator returns constant objects when dereferenced. Whether + the iterator object itself is const has nothing to do with + whether the iterator is mutable or constant. This is analogous to + the way built-in pointer types behave. For example, one can + modify objects pointed to by a const pointer +

+    int* const x = new int;
+    int i = 3;
+    *x = i;
+
+ but one cannot modify objects pointed to by a pointer + to const +
+    int const* x = new int;
+    int i = 3;
+    *x = i;
+
+

Revised @@ -871,7 +899,7 @@ bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, - @@ -887,5 +915,7 @@ bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, +