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

inherit both iterator and std::vector?

P: n/a
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
--
Ernst Murnleitner
www.awite.com
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a

"Ernst Murnleitner" <mu*@awite.de> wrote in message
news:41**********************@news.freenet.de...
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
Jul 22 '05 #3

P: n/a
"Ernst Murnleitner" <mu*@awite.de> wrote in message
news:41***********************@news.freenet.de...

[snip]


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
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
Ernst Murnleitner wrote:
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.

(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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.