473,881 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

example vector wrapper

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
5 4883
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
On 19 Mar, 05:44, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
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
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
On 19 Mar, 05:44, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
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::iterator s 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>::iter ator it_;
my_iterator(vec tor<A>::iterato r it)
: it_(it)
{}

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

my_iterator(){}

void increment() { ++it_; }

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

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

now you can write:

std::for_each(m y_iterator(v.be gin()), my_iterator(v.e nd())
, bind(&fun, _1));
Mar 19 '07 #5
On Mar 19, 5:35 am, "dasjotre" <dasjo...@googl email.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::iterator s is perfect for that.
Indeed, boost::iterator _facade slimmed down my code and gave me proper
iterators. Thanks!

Mar 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3960
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
9
2314
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); //???? Required ??????????????//
5
2589
by: ma740988 | last post by:
Hopefully I'm not asking too much here, nontheless consider the test source: #include <vector> #include <utility> using std::vector; using std::pair; #define INVALID_ID -1
8
3556
by: Bo Peng | last post by:
Dear list, I am using std::vector<bool> (bit_vector) to store my bit sequence. To access the same sequence from C (to expose to a python module), I need to know the pointer and offset of vector::<bool>::iterator (or reference). However, given a std::vector<bool> a, all a.begin(), a etc are instances of a proxy class so I can not do things like &*a.begin(). Is there a safe way to get the information I need? Many thanks in advance.
2
2106
by: Marcus | last post by:
I have a vector within a vector within a map. The innermost vector houses a struct with some ints and floats. The map keys off the secondary vector which acts as a wrapper for multiple vector struct entries for a specific key. std::vector<Object> vec; std::vector<std::vector <Object> > vecMain; std::map<std::string,std::vector <std::vector <Object> > > map; //Assuming I have loaded the vectors and map, how do I get at the vec
11
1930
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
7
2540
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
4
2552
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type vector's Iterator in C#? The problem is in the next line and the rest of the code is just additional information on what I'm doing. Thanks. CUnityDS.DE_ERRORS errcode = CUnityDS.LibWrap.EncodeAsnUser(ref blob, userContextData); //In & out...
4
1709
by: Christian Schmidt | last post by:
Hi all, I'm trying to implement a std::vector-like wrapper for IList. The hard part seems to be operator, because it returns an unmanaged reference. Probably I have to use pin_ptr to achieve this, but I don't know how. Can anybody help? Thanks, Christian
0
9930
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9555
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.