iterator design help

R

Rex_chaos

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.
 
V

Victor Bazarov

Rex_chaos said:
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
 
R

Rex_chaos

template said:
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 {...}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
V

Victor Bazarov

Rex_chaos said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

stl::map iterator 2
Design one iterator class 5
Iterator in a template 4
Indexed list 0
iterator help 1
Casting STL's const_iterator to iterator 6
Pointer arithmetic 4
returning an iterator 'concept' 4

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top