473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

inherit both iterator and std::vector?

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
5 2562
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

"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
3
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.