inherit both iterator and std::vector?

E

Ernst Murnleitner

Hello,

is it possible to derive from std::vector and derive also its iterator?

If I do it like in the example below, I get a problem when I need the begin
of the vector:

begin() returns the derived iterator. I need the derived iterator in order
to overload the operator++. But I want to use also
std::vector<PBaseItem>::begin() in order to get the begin of the vector.
But this function delivers the iterator of the base class. Is there any way
to solve this? (One way would be to use a begin which uses a pointer to an
iterator).



Example:

class CBaseItem;
// PBaseItem is a smart pointer pointing to a class CBaseItem:
typedef boost::intrusive_ptr<CBaseItem> PBaseItem;

class CBaseItemVector : public std::vector<PBaseItem> {
public:
CBaseItemVector(){};
virtual ~CBaseItemVector(){};

class iterator : public std::vector<PBaseItem> {
public:
iterator (){};
virtual ~iterator(){};
virtual PBaseItem operator*();
virtual void operator++();
virtual void operator++(int);
};

virtual iterator begin();

};


I would be interested if someone could give me a hint.

Greetings

Ernst
 
E

Ernst Murnleitner

Of cource, instead of intheritance I could use member variables
(std::vector<...>::iterator as member of the iterator of the derived class
instead deriving from it).


Ernst Murnleitner
www.awite.com
 
J

John Harrison

Ernst Murnleitner said:
Of cource, instead of intheritance I could use member variables
(std::vector<...>::iterator as member of the iterator of the derived class
instead deriving from it).

Absolutely. And you should not inherit from std::vector either. What exactly
is it that you are trying to accomplish?

john
 
A

Aguilar, James

Ernst Murnleitner said:

The idea doesn't seem to make logical sense. The iterator is different from
the thing it iterates through. Just because you can bend over backwards to
provide both functionalities in the same class doesn't mean that you should.

James
 
E

Ernst Murnleitner

Hello John
Absolutely. And you should not inherit from std::vector either. What
exactly is it that you are trying to accomplish?

Why should I not derive from std::vector?

I have some vectors with pointers to a base class. The objects where the
pointers point to are all derived from the base class but are different
objects. Now I wanted to define my own iterator where the operator++ should
go from one kind of item to the next. I wanted to give the constructor of
the iterator a parameter (enum) in order to define, which items it should
enumerate.

But when I looked closer to it, i found that I had to define a new vector
(derive from or use std::vector) in order to use a different iterator.

Now I found it is much less efford to put the needed functions into the base
class.

Greetings

Ernst
 
K

Kai-Uwe Bux

Ernst said:
Hello John


Why should I not derive from std::vector?

I have some vectors with pointers to a base class. The objects where the
pointers point to are all derived from the base class but are different
objects. Now I wanted to define my own iterator where the operator++
should go from one kind of item to the next. I wanted to give the
constructor of the iterator a parameter (enum) in order to define, which
items it should enumerate.

(derive from or use std::vector) in order to use a different iterator.

Now I found it is much less efford to put the needed functions into the
base class.

std::vector<> has no virtual destructor. This is the reason that many
regulars in this group frown upon inheriting from standard containers: it
opens up the possibility for using std::vector<>* polymorphically, which in
turn triggers errors that are hard to find and trafic in this news group
that could easily be avoided.

That said, you can inherit from std::vector<>. In your case, the idea would
be to extend or modify the interface. Now, as for iterators, you would have
to change all routines returning iterators to use the new ones. Also
routines taking iterators as aruments will not use your overloaded
definitions internally since methods of iterators are not virtual. Thus
your savings by deriving from the iterator seem somewhat limited.

What about defining a plain old function

std::vector<T*>::iteraror
next_of_a_kind( std::vector<T*>::iteraror from, KindType what_kind )

If you really need an iterator, e.g. for the use in generic algorithms, you
coud define such an iterator outside of the container class, at the expense
of explicitly converting. Less magic, less work, less error prone.


Best

Kai-Uwe Bux
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top