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