example vector wrapper

T

Tim H

I'm working on a class that wraps a vector. In fact, it behaves very
much like a vector. I've read it's "bad" to subclass vector, since it
is not intended for that use. So I am wrapping it. I have a lot of
simple pass-thru methods, which is fine.

Where it all breaks down is iterators. The underlying vector is
storing annotated data that the caller does not need to see. Think of
it this way:

template <class T>
class helper { ... };

template <class T>
class vector_wrapper {
....
vector< helper<T> > real_vector;
....
};

Making vector_wrapper iterate in all the same ways as a regular vector
is just falling down. I find myself hacking and slashing to get const
iterators to actually be const and reverse iterators to work, etc.

Does anyone have an example of a class that wraps vector and provides
full iterability?

Tim
 
K

kwikius

I'm working on a class that wraps a vector. In fact, it behaves very
much like a vector. I've read it's "bad" to subclass vector, since it
is not intended for that use. So I am wrapping it. I have a lot of
simple pass-thru methods, which is fine.

Where it all breaks down is iterators. The underlying vector is
storing annotated data that the caller does not need to see. Think of
it this way:

template <class T>
class helper { ... };

template <class T>
class vector_wrapper {
...
vector< helper<T> > real_vector;
...

};

Making vector_wrapper iterate in all the same ways as a regular vector
is just falling down. I find myself hacking and slashing to get const
iterators to actually be const and reverse iterators to work, etc.

Does anyone have an example of a class that wraps vector and provides
full iterability?

Can't help, but can sympathise. I recently decide to create a
container with conforming iterators, but in practise found it much
simpler to write overloads of functions for my containers... probably
not recommended though.

Anyway I decided that in future I would just try to live with the
provided containers wherever possible and not write my own unless
absolutely necessary

Anyway to do things "the right way"
You probably need to consult section 24 of the C standard (Iterators
library) and be warned... its heavy reading.

Also of interest might be The boost iterator adaptors. I have never
really figured what these are about, but they may provide some
insights:

http://www.boost.org/libs/iterator/doc/index.html

Sorry I cant be more help....

regards
Andy Little
 
K

kwikius

Can't help, but can sympathise. I recently decide to create a
container with conforming iterators, but in practise found it much
simpler to write overloads of functions for my containers... probably
not recommended though.

Anyway I decided that in future I would just try to live with the
provided containers wherever possible and not write my own unless
absolutely necessary

Anyway to do things "the right way"
You probably need to consult section 24 of the C standard

Ooops... C++ standard ;-) ^^^

regards
Andy Little
 
G

Gianni Mariani

Tim said:
I'm working on a class that wraps a vector. In fact, it behaves very
much like a vector. I've read it's "bad" to subclass vector, since it
is not intended for that use.

Pure hogwash.

... So I am wrapping it. I have a lot of
simple pass-thru methods, which is fine.

Go ahead, derive from vector.
 
D

dasjotre

std containers don't have virtual destructor.

struct A : public vector<int>
{
~A(){ do something very important }
};
vector<int> * p = new A;
....
delete p;
this will not call A's destructor.

If you don't use it this way it is perfectly
fine to subclass any of the std containers.
Also of interest might be The boost iterator adaptors. I have never
really figured what these are about, but they may provide some
insights:

http://www.boost.org/libs/iterator/doc/index.html

It seems to me that you don't want to wrap a vector
but just its iterator. boost::iterators is perfect for that.

simple example:

void fun(int);

struct A
{
int n;
};

vector<A> v;

now I want to iterate through v and execute fun
for each A. since fun takes int I need to adapt
vector's iterator

struct my_iterator : public boost::iterator_facade
< my_iterator
// deference type
, int
// we only want to go one way
, boost::forward_traversal_tag >
{
vector<A>::iterator it_;
my_iterator(vector<A>::iterator it)
: it_(it)
{}

// you need to implement these methods
// to satisfy iterator_facade's requirements.

my_iterator(){}

void increment() { ++it_; }

bool equal(my_iterator const& other) const
{
return it_ == other.it_;
}

reference_type dereference() const
{
return (*it_).n;
}
};

now you can write:

std::for_each(my_iterator(v.begin()), my_iterator(v.end())
, bind(&fun, _1));
 
T

Tim H

It seems to me that you don't want to wrap a vector
but just its iterator. boost::iterators is perfect for that.

Indeed, boost::iterator_facade slimmed down my code and gave me proper
iterators. Thanks!
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top