Put a stl container in private or public?

G

Goran

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
 
B

Barry

Goran said:
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()
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
S

SasQ

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top