Inherit from vector, encapsulate, or not bother?

N

nw

Hi All,

I currently have a vector of objects (lets call them MyObject). I want
to perform various operations regularly on the whole vector, for
example find the maximum, average, or operations not dissimilar to
that. So as I see it I have 3 options:

1. Implement these operations as functions
2. Derive a class from vector (from googling people seem to think this
is a bad idea)
3. Encapsulate the vector in an object.

I'm tending toward option 1 at the moment and perhaps making my
functions generic?

What does comp.lang.c++ think?

Any advice appreciated!

#include <iostream>
#include <vector>

using namespace std;

class MyObject {

int v1;
int v2;
int v3;
};

class MyObjectSet {
public:

vector<MyObject> vec;

int max() {
// Would find maximum value in o

return 1;
}
};

int main() {
MyObject o;

MyObjectSet s;

s.vec.push_back(o);

}
 
C

Christopher

Hi All,

I currently have a vector of objects (lets call them MyObject). I want
to perform various operations regularly on the whole vector, for
example find the maximum, average, or operations not dissimilar to
that. So as I see it I have 3 options:

1. Implement these operations as functions
2. Derive a class from vector (from googling people seem to think this
is a bad idea)
3. Encapsulate the vector in an object.

I'm tending toward option 1 at the moment and perhaps making my
functions generic?

What does comp.lang.c++ think?

Any advice appreciated!

#include <iostream>
#include <vector>

using namespace std;

class MyObject {

int v1;
int v2;
int v3;

};

class MyObjectSet {
public:

vector<MyObject> vec;

int max() {
// Would find maximum value in o

return 1;
}

};

int main() {
MyObject o;

MyObjectSet s;

s.vec.push_back(o);

}

You should, IMO, encapsulate the vector in a class and write methods
that make the calculations

class MyContainer
{
std::vector

public:
MyContainer();
~MyContainer();

GetAverage();
GetMax();
};

or

Write template functions that just take a begin and end iterator,
along with a user defined functor for getting a integral value from
the class type.

I would also be sure I am not writing anything over again that already
exists in <algorithm>
 
J

Joe Greer

It depends. If your new container is not going to "specialise" the
vector (so to speak), then derive away. Inheritance is a mechanism
and the usefulness of it is in the eye of the beholder.

To expand a bit on this. The "problem" with inheriting from std::vector is
that it doesn't have a virtual interface and especially it doesn't have a
virtual destructor. If you don't add any additional state information,
then it doesn't matter much, but if you do add more variables and maybe
tweak a method or two, then you are in a situation where you can
"accidentally" pass your new vector to a routine which expects a
std::vector and things can go really bad. For example, the routine swaps
your vector with a new one in the attempt to implement strong type safety.
Now your new vector gets destroyed with the std destructor and you leak
memory, fault or something more subtle. Bad news. On the otherhand, if
you are just adding some functions and just want a way to keep them all in
one spot, then you are probably ok. Delegation and private inheritance
solve this by not letting you pass your new collection as a std::vector.

joe
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top