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: Why no std::back_insert_iterator::value_type? - comp.lang.c++ ...// Deleted headers. using namespace std; int main() { typedef vector<int> IntVec; typedef back_insert_iterator<IntVec ... Otherwise we (both you and I) are just losing ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... recommend to follow this route, it is both portable ... new type as a parameter to ostream_iterator. std ... Passing a std::vector pointer as a long ??? - comp.lang ... Writing operator<< for std::vector - comp.lang.c++.moderated ...If you have an std::vector< ns1::Object1 > as a parameter, Koenig lookup will search in both std and ns1. ... out) { for(typename std::vector<X>::const_iterator ... iterator problem with List of Files - comp.lang.java.programmer ...Problems with a std::vector (begin and end): Different behavior ..... is a ... comp.lang.python... same thing with: map(value_function, value_iterator) and avoid both ... New form of equal_range() - comp.lang.c++.moderatedI can do vector<int> v; pair<vector<int>::iterator, vector ... Hint: std::vector<unsigned> v; std::equal_range ... You can't have both. What about the mathematical ... Const constructor - comp.lang.c++.moderated... only designs that were mentioned in this thread are Fabio's class A and iterators. Both ... called eg called Alloc) which holds an array with copy semantics (like std::vector ... Reading text files, std::atoi, Char Variables - comp.lang.c++ ...... read_data(std::istream &in,std::string &name,std::vector ... In both cases, you would then use istringstream ... istream_iterator<int>(in), > std::istream_iterator<int>(), std ... writing robust software? - comp.lang.c++.moderatedWith C++ templates and for_each, or iterators, you can ... the new range-based for statement as in: std::vector<T> v ... Definition of robust The more that programs have both ... Improving a short program in C++ - comp.lang.c++.moderated ...... for example: typedef std::vector<int ... Actually, both 'vertices' and 'n' can be replaced by a vector<Vertex*>. ... begin(), adjacents.end(), std::ostream_iterator ... Texture disappears? - comp.graphics.api.opengl... Are you putting the polygons in a std::vector? If the ... when I was getting each Poly* out of the vector, I used an iterator. ... file can be specified to texture one or both ... inherit both iterator and std::vector? - C / C++inherit both iterator and std::vector?. C / C++ Forums on Bytes. Is it generally safe to inherit from STL iterator classes? - C / C++inherit both iterator and std::vector? STL Question: Safe to use elements after erasing them from the collection? iterator help; Iterator doubts, Decision on Iterator usage 7/2/2012 3:27:28 AM
|