re*******@21cn. com (Rex_chaos) wrote:
I am writing my own container and need an iterator. I am hesitating
if I should my iterator should inherited from std::iterator or just
write my own one. Please give me an idea.
'std::iterator< >' just typedefs a bunch of types. That's it. If you
don't derive from 'std::iterator< >' you should be sure to [partially]
specialize 'std::iterator_ traits<>' for your iterator type because
otherwise the algorithms won't work OK. Due to lack of partial
specialization support with some compilers, it is probably a good idea
to derive from 'std::iterator< >' anyway: originally, this was mostly a
work around.
BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent class.
This one is a little bit tricky. First of all, it does not matter to
user. Since nested classes have no additional rights external class
have, nesting the iterator class only serves a documentation purpose.
With some specialization I found that it is sufficient to specialize
the container while leaving the iterator alone. Also, the semantics of
making an external class a friend are well defined in the standard
which does not apply to nested classes. Thus, I tend to make iterators
external classes. There is, however, no strong argument I'm aware of
which prefers one of the approaches.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>