inherit both iterator and std::vector?

  • Follow


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
0
Reply mur (18) 9/17/2004 10:17:09 PM

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
0
Reply mur (18) 9/17/2004 10:53:18 PM


"Ernst Murnleitner" <mur@awite.de> wrote in message 
news:414b6a57$0$6335$9b622d9e@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


0
Reply john_andronicus (3950) 9/18/2004 6:05:53 AM

"Ernst Murnleitner" <mur@awite.de> wrote in message 
news:414b61de$0$13035$9b622d9e@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 


0
Reply jfa1 (63) 9/18/2004 7:09:12 AM

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  
0
Reply mur (18) 9/18/2004 7:57:22 PM

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

0
Reply jkherciueh (3186) 9/20/2004 1:32:29 AM

5 Replies
42 Views

(page loaded in 5.589 seconds)

Similiar Articles:













7/2/2012 3:27:28 AM


Reply: