Hi,
I've been playing around a bit wist custom random-acccess
iterators (just for my own education and the fun of it) and
read that such an iterator must have an offset dereference
operator ([]). Does that imply that I have to define such an
operator method in the iterator? Looking at what happens when
I have e.g.
Custom_Container cc;
Custom_Container< int >::iterator ccit = cc.begin();
and later, once the container has acquired some elements, doing
e.g.
std::cout << ccit[ 1 ] << std::endl;
I found that an overloaded '[]' operator in the iterator
neither seemed to be necessary nor was it called if defined.
Since 'x[n]' is just syntactic sugar for '*(x+n)' I'm tempted
to assume that having the required '+' and the dereferencing
operator is all that's actually needed. Looking at several
examples of implementations of custom iterators by others (of
which I can't assess their quality) I find that some define a
'[]' operator and others don't, leaving me a bit puzzled what
is required. If it's defined it usually boils down to either
T & operator [ ] ( int n ) { return m_ptr[ n ]; }
or
T & operator [ ] ( int n ) { return *( m_ptr + n ); }
which looks a bit superfluous. Or was having such an overload
somthing required in pre-C++11 days but is now not needed any-
more?
Thanks and best regards, Jens
--
\ Jens Thoms Toerring ___
j...@toerring.de
\__________________________
http://toerring.de