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

example vector wrapper

P: n/a
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

Mar 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 19 Mar, 03:31, "Tim H" <thoc...@gmail.comwrote:
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
Mar 19 '07 #2

P: n/a
On 19 Mar, 05:44, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 19 Mar, 03:31, "Tim H" <thoc...@gmail.comwrote:


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
Ooops... C++ standard ;-) ^^^

regards
Andy Little
Mar 19 '07 #3

P: n/a
Tim H wrote:
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.

Mar 19 '07 #4

P: n/a
On 19 Mar, 05:44, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 19 Mar, 03:31, "Tim H" <thoc...@gmail.comwrote:

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.
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.
>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:

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<Av;

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));
Mar 19 '07 #5

P: n/a
On Mar 19, 5:35 am, "dasjotre" <dasjo...@googlemail.comwrote:
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.
Indeed, boost::iterator_facade slimmed down my code and gave me proper
iterators. Thanks!

Mar 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.