Inherit from vector, encapsulate, or not bother?

Discussion in 'C++' started by nw, Apr 16, 2008.

  1. nw

    nw Guest

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

    }
     
    nw, Apr 16, 2008
    #1
    1. Advertising

  2. nw

    Christopher Guest

    On Apr 16, 10:05 am, nw <> 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>
     
    Christopher, Apr 16, 2008
    #2
    1. Advertising

  3. nw

    Joe Greer Guest

    "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
     
    Joe Greer, Apr 16, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    1
    Views:
    679
    Eliyahu Goldin
    Oct 6, 2005
  2. mosfet
    Replies:
    1
    Views:
    1,021
    Mike Wahler
    Jul 4, 2003
  3. Java_New
    Replies:
    1
    Views:
    392
    Andrew Thompson
    Feb 3, 2007
  4. Replies:
    8
    Views:
    1,962
    Csaba
    Feb 18, 2006
  5. TBass
    Replies:
    6
    Views:
    338
    mlimber
    Jun 21, 2006
Loading...

Share This Page