473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Put a stl container in private or public?

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

Aug 29 '07 #1
3 1469
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
Aug 29 '07 #2
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
Aug 29 '07 #3
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
Aug 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shaun Marshall | last post by:
Hope someone can help, for an assignment i was given a starter application package, with that starter package i had to create some employee classes and test harnesses. below iv pasted my Container...
8
by: Markus Dehmann | last post by:
I defined a base class in order to put heterogeneous values into a standard container: All values that I store in the container are derived from my base class. Now when I iterate over the...
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
3
by: Valeriu Catina | last post by:
Hi, I have an array class (the blitz library actually) which looks like this: template<typename P_numtype, int N_rank> class Array : public MemoryBlockReference<P_numtype> , public...
9
by: axel22 | last post by:
Hello, I have the following problem. I create a class called MyClass which includes another class calles MyContainer. Class MyContainer has a member which is a vector, instantiated as...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
3
by: gallows | last post by:
The container is: template <typename T> class Container { public: // container methods.. // iterator: class const_iterator { public:
1
by: scrier | last post by:
Hi, I wish to have a baseclass A that class B and C inherits from, C is a standard class that has some get and set functions while B is like C only with a container class that can have 1 or...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.