Put a stl container in private or public?

Discussion in 'C++' started by Goran, Aug 29, 2007.

  1. Goran

    Goran Guest

    Hi all,

    I'm asking me how to save a std::vector or std::set in my object?

    Should I put it in private or public? And if in private... how looks
    like the public methods to access the elements in the container?

    My first attempt was to put them in private and build some public
    methods...

    [...]

    public:
    void AddObj(const Obj_t * aObj);
    void EraseObj(unsigned int aPosition);

    public:
    unsigned int GetObjQuantum() const;
    Obj_t GetObj(unsigned int aPosition) const;

    private:
    std::set<Obj_t *> * itsObjs;

    I don't feel happy with solution above but I still don't know a better
    way.

    Thanks

    Goran
    Goran, Aug 29, 2007
    #1
    1. Advertising

  2. Goran

    Barry Guest

    Goran wrote:
    > Hi all,
    >
    > I'm asking me how to save a std::vector or std::set in my object?
    >
    > Should I put it in private or public? And if in private... how looks
    > like the public methods to access the elements in the container?
    >
    > My first attempt was to put them in private and build some public
    > methods...
    >
    > [...]
    >
    > public:
    > void AddObj(const Obj_t * aObj);
    > void EraseObj(unsigned int aPosition);
    >
    > public:
    > unsigned int GetObjQuantum() const;
    > Obj_t GetObj(unsigned int aPosition) const;
    >
    > private:
    > std::set<Obj_t *> * itsObjs;
    >
    > I don't feel happy with solution above but I still don't know a better
    > way.
    >
    > Thanks
    >
    > Goran
    >


    Making a data member public is generally wrong idea.

    Well, creating a wrapper class the way you do is nothing wrong,
    what you complain is you have to add public member to meet your each new
    operation of the *set*, yourself still don't know what exactly
    operations you will need. So I guess you still haven't done enough
    engineering before you doing your code

    Well, there one simpler way, but not good one
    provide
    std::set<Obj_t *> const* GetSet() const
    and
    std::set<Obj_t *>* GetSet()

    --
    Thanks
    Barry
    Barry, Aug 29, 2007
    #2
    1. Advertising

  3. On 2007-08-29 02:32, Goran wrote:
    > Hi all,
    >
    > I'm asking me how to save a std::vector or std::set in my object?
    >
    > Should I put it in private or public? And if in private... how looks
    > like the public methods to access the elements in the container?
    >
    > My first attempt was to put them in private and build some public
    > methods...
    >
    > [...]
    >
    > public:
    > void AddObj(const Obj_t * aObj);


    Ask yourself if you need to store pointers to the objects or if you can
    use copies of them instead.

    > void EraseObj(unsigned int aPosition);


    When using a set the concept of position is a bit vague, since an
    object's position can change when other objects are inserted.

    >
    > public:
    > unsigned int GetObjQuantum() const;
    > Obj_t GetObj(unsigned int aPosition) const;
    >
    > private:
    > std::set<Obj_t *> * itsObjs;


    Usually there's no need to make this a pointer, use a normal instance.

    >
    > I don't feel happy with solution above but I still don't know a better
    > way.


    If you just want a class to be a container, use one of the standard
    containers, don't wrap it. If not you usually only need to implement a
    few functions, like add, remove, and get, and then your design looks
    quite good.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 29, 2007
    #3
  4. Goran

    SasQ Guest

    On Tue, 28 Aug 2007 17:32:31 -0700, Goran wrote:

    > public:
    > void AddObj(const Obj_t * aObj);
    > void EraseObj(unsigned int aPosition);
    > [...]
    > I don't feel happy with solution above but I still don't know
    > a better way.


    Maybe try to derive it non-publicly and publish
    only the methods you want, by redeclaring them in
    public section?

    But I think inheritance isn't a good idea, because
    standard containers are not designed for inheritance.

    --
    SasQ
    SasQ, Aug 29, 2007
    #4
    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.

Share This Page