Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inherit from vector, encapsulate, or not bother?

Reply
Thread Tools

Inherit from vector, encapsulate, or not bother?

 
 
nw
Guest
Posts: n/a
 
      04-16-2008
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);

}
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 10:05 am, nw <n...@soton.ac.uk> wrote:
> 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>
 
Reply With Quote
 
 
 
 
Joe Greer
Guest
Posts: n/a
 
      04-16-2008
"Victor Bazarov" <> wrote 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there any reason not to inherit from std::exception __PPS__ C++ 3 11-09-2005 07:50 PM
How to Adding Functionality to a Class by metaclass(not by inherit) kyo guan Python 1 08-12-2005 04:55 AM
How to Adding Functionality to a Class by metaclass(not by inherit) Kyo Guan Python 0 08-12-2005 04:22 AM
Inherit Datagrid : Active schema does not support element.. RJN ASP .Net 1 09-30-2004 03:05 PM
To inherit or not to bobsled C++ 7 04-26-2004 12:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57