473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Inherit from vector, encapsulate, or not bother?

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

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

return 1;
}
};

int main() {
MyObject o;

MyObjectSet s;

s.vec.push_back(o);

}
Jun 27 '08 #1
4 2547
On Apr 16, 10:05 am, nw <n...@soton.ac.ukwrote:
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>
Jun 27 '08 #2
nw wrote:
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)...
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.
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.
I'm tending toward option 1 at the moment and perhaps making my
functions generic?
Sounds like a good idea.
What does comp.lang.c++ think?
<shrugWe never seem to think exactly the same thing... <g>
>
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
Jun 27 '08 #3
"Victor Bazarov" <v.********@comAcast.netwrote in news:fu55l2$gmc$1
@news.datemas.de:
>
>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
Jun 27 '08 #4
Joe Greer wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in news:fu55l2$gmc$1
@news.datemas.de:
>>
>>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
Jun 27 '08 #5

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

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
3
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
11
by: TG | last post by:
Hi there. I'm trying to create a simple class called Vector which inherit from array. class Vector(array): def __init__(self,length): """initialize a vector of random floats of size length....
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
3
by: Greg | last post by:
How do I inherit or templatise from a continer class, without having to reinterate (type) the member functions ?
19
by: Ramon F Herrera | last post by:
Newbie alert: I come from the C side, struggling to learn C++. I need a two-dimensional data structure. I first tried a regular vector and added the 2nd dimension with my own structs and...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.