By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

iterator design help

P: n/a
Hi there,
I am writing an iterator for a container. Just like a typical
iterator,

template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}

MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}
MyIter& operator++() {...; return *this;}
...
};

While MyIter is applied to a template algorithm like std::transform,
the copy constructor will be actived because of the reference to cont.
It lowers effeciency a lot. Maybe I should use a pointer as instead
for speed purpose

....
MyContainer<ADT>* refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
....

However, it is another problem. Suppose the container passed to the
iterator has been released, refc will then become an invalid pointer
!!! I can't release the pointer (refc) in the destructor because I
don't know if any object own the container.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Rex_chaos" <re*******@21cn.com> wrote...
I am writing an iterator for a container. Just like a typical
iterator,

template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}

MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}
I think you're confused. "begin" and "end" should be members of
the container, not of the iterator.
MyIter& operator++() {...; return *this;}
...
};

While MyIter is applied to a template algorithm like std::transform,
the copy constructor will be actived because of the reference to cont.
I am not sure I understand the sentence above.
It lowers effeciency a lot. Maybe I should use a pointer as instead
for speed purpose

...
MyContainer<ADT>* refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}
...

However, it is another problem. Suppose the container passed to the
iterator has been released, refc will then become an invalid pointer
!!! I can't release the pointer (refc) in the destructor because I
don't know if any object own the container.


That's the problem with all iterators. They become invalid when the
container they were iterating is destroyed. Simply document that
behaviour and think no more of it.

Victor
Jul 19 '05 #2

P: n/a
> > template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}

MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}


I think you're confused. "begin" and "end" should be members of
the container, not of the iterator.

Thanks for your reply. Yes, I should put "begin" and "end" inside the
container. However, it's something confusing me. Should I return a
reference or just a value of the iterator? i.e Which one is better?

MyIter& begin() {...; return *this;} // defined in MyContainer
MyIter& operator++() {...; return *this;} // defined in MyIter

or

MyIter begin() {...; return *this;} // defined in MyContainer
MyIter operator++() {...; return *this;} // defined in MyIter

Should I have "const" here like

MyIter& begin() const {...}
Jul 19 '05 #3

P: n/a
Rex_chaos escribió:
MyIter begin() {...; return *this;} // defined in MyContainer
MyIter operator++() {...; return *this;} // defined in MyIter

Should I have "const" here like

MyIter& begin() const {...}


You probably need:

MyIter begin ()

And

MyConstIter begin () const

Regards.
Jul 19 '05 #4

P: n/a
"Rex_chaos" <re*******@21cn.com> wrote in message
news:f7*************************@posting.google.co m...
template <typename ADT>
class MyIter
{
...
MyContainer<ADT>& refc;
public:
MyIter(MyContainer<ADT>& cont) :refc(a) {...}

MyIter& begin() {...; return *this;}
MyIter& end() {...; return *this;}
I think you're confused. "begin" and "end" should be members of
the container, not of the iterator.

Thanks for your reply. Yes, I should put "begin" and "end" inside the
container. However, it's something confusing me. Should I return a
reference or just a value of the iterator? i.e Which one is better?


How can you return a reference? A reference to what? Are you
going to keep _the_only_ iterator in a container? What if I need
two independent iterators? Just think about it for a minute.
MyIter& begin() {...; return *this;} // defined in MyContainer
MyIter& operator++() {...; return *this;} // defined in MyIter

or

MyIter begin() {...; return *this;} // defined in MyContainer
MyIter operator++() {...; return *this;} // defined in MyIter

Should I have "const" here like

MyIter& begin() const {...}


Just like Julian suggested, you should have a special MyConstIter
complementing MyIter.

Get the Josuttis' book on the Standard library and read about
iterators. It will be time well spent.

Victor

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.