Connecting Tech Pros Worldwide Help | Site Map

Inherit from vector, encapsulate, or not bother?

nw
Guest
 
Posts: n/a
#1: Jun 27 '08
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<MyObjectvec;

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

return 1;
}
};

int main() {
MyObject o;

MyObjectSet s;

s.vec.push_back(o);

}
Christopher
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Inherit from vector, encapsulate, or not bother?


On Apr 16, 10:05 am, nw <n...@soton.ac.ukwrote:
Quote:
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<MyObjectvec;
>
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>
Victor Bazarov
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Inherit from vector, encapsulate, or not bother?


nw wrote:
Quote:
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
Looks like a very good first step. If you find it unnecessary to do
either of the two below, you just leave those alone and keep using them
and if you decide to have your own container class, you can still
incorporate those as member functions (or call them from some member
functions)...
Quote:
2. Derive a class from vector (from googling people seem to think this
is a bad idea)
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.
Quote:
3. Encapsulate the vector in an object.
....or privately inherit the vector. Either way, you're implementing
your object "in terms of the vector". There is a bit more freedom
in that (versus deriving), but, again, it's debatable, and depends.
Quote:
I'm tending toward option 1 at the moment and perhaps making my
functions generic?
Sounds like a good idea.
Quote:
What does comp.lang.c++ think?
<shrugWe never seem to think exactly the same thing... <g>
Quote:
>
Any advice appreciated!
>
#include <iostream>
#include <vector>
>
using namespace std;
>
class MyObject {
>
int v1;
int v2;
int v3;
};
>
class MyObjectSet {
public:
>
vector<MyObjectvec;
>
int max() {
// Would find maximum value in o
>
return 1;
}
};
>
int main() {
MyObject o;
>
MyObjectSet s;
>
s.vec.push_back(o);
>
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Joe Greer
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Inherit from vector, encapsulate, or not bother?


"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fu55l2$gmc$1
@news.datemas.de:
Quote:
>
Quote:
>2. Derive a class from vector (from googling people seem to think this
>is a bad idea)
>
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
Victor Bazarov
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Inherit from vector, encapsulate, or not bother?


Joe Greer wrote:
Quote:
"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fu55l2$gmc$1
@news.datemas.de:
>
Quote:
>>
Quote:
>>2. Derive a class from vector (from googling people seem to think
>>this is a bad idea)
>>
>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.
.... at a price of having to re-implement all member functions from
the contained (or privately inherited) std::vector, often in very
simple terms of the std::vector member functions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Closed Thread