Want to Overload ++ and -- operators for list iterator

R

Rohit.MEHTA

Hi All

I want to overload the ++ and - operator for stl list iterator so that
I could do some thing additional in that
As want to set the current node status also

Can some one please post sample code to use this, I would NOT like to
do it by inheriting the iterator class. Rather I would like to keep a
member iterator and wrap the logic around it

Regards
 
R

Ron Natalie

Hi All

I want to overload the ++ and - operator for stl list iterator so that
I could do some thing additional in that
As want to set the current node status also

Can some one please post sample code to use this, I would NOT like to
do it by inheriting the iterator class. Rather I would like to keep a
member iterator and wrap the logic around it
Nothing requires iterators to be classes.

There's no provision for "replacing" the existing operators
for these. The only way to do it is to define your own
iterator class.
 
D

dasjotre

Hi All

I want to overload the ++ and - operator for stl list iterator so that
I could do some thing additional in that
As want to set the current node status also

Can some one please post sample code to use this, I would NOT like to
do it by inheriting the iterator class. Rather I would like to keep a
member iterator and wrap the logic around it

template<class T, class AllocT>
class my_iterator
{
typedef list<T, AllocT> list_t;
public:
typedef typename list_t::value_type value_type;
typedef typename list_t::pointer pointer;
typedef typename list_t::reference reference;
...

my_iterator(base_t & b) : base_(b) {}
my_iterator operator ++ ()
{
++base_;
// do what you want
return *this;
}
bool operator != (my_iterator const & b) const
{
return base_ != b.base_;
}
reference operator * ()
{
return *base_;
}
private:
typedef typename list_t::iterator base_t;
base_t base_;
};
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top