Solved

What is the simplest cleanest way to implement a container?

Posted on 2000-04-06
10
177 Views
Last Modified: 2010-04-02
I want a global bag that I can throw things into
and then take them out later.  

The simplest contatiner that I can imagine.
(OK, surely there are simpler ones).
I want to put things into this bag in phase 1.
I will then read things from this bag in phase 2.

Write once.  Read once.  I don't care about
efficiency (spped or memory).  I just
want the simplest/cleanest interface.

Should I build my own container class on top of
one of STL's containter classes?  

My gut feel is that this ought to be one line in my .h
file, soemthing like:

extern slist<ObjectClass> ObjectContainer;

and a line in a .cc file, something like:

 slist<ObjectClass> ObjectContainer;

Possibly with an initialization to a null slist somewhere.

Then, in phase 1, I use:  ObjectContainer.pushback( ken)  to put ken in.  And, in phase 2, I use
an ObjectContainer iterator along with
 ObjectContainer.begin(), .next() and .end()
to read out all the objects.

Am I mising something?

Thanks
  Ken
0
Comment
Question by:klopter
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 4

Accepted Solution

by:
abancroft earned 50 total points
Comment Utility
Using one of the STL containers is probably the simplest (and standard) method. You don't need to initialise it to a 'null slist' anywhere.

Some things to consider:
1. Do you want duplicate objects in the container?
2. Will you be iterating bi-directionaly?
3. What are you storing in the container? STL requires that the objects have default & copy constructors and assignment operators.
0
 
LVL 4

Expert Comment

by:abancroft
Comment Utility
4. Do you know how many objects you'll be storing before you start adding them?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Are we talking about the same "type of things", i.e. same classes, in each case.  i.e. will you have on container for class Widgits and one container for class Digits etc.?    If not, things get harder.  (Especiually if you are a polymorphism whimp. :-) )

If that is the case, then I woudl say you should use on the STL classes directly, or provide a thin wrapper around it.  There is no need to a heavy wrapper around the STL class, as it propides the type of behavior you want.  i.e you can leave data in the container class and copy it out of the container as many times as you like, like

vector<Widget> C;

Widget W1;
C.push_back(W1);

Widget W2 = C[0];
Widget W3 = C[0];
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
abancroft, you probably should have answered.  I will withdraw mine.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> Should I build my own container class on top of
>> one of STL's containter classes?
I often like to do that because I find the STL syntax ugly and I often want my container to have a more limited set of behaviors.  So I often create a class that has that STL class (I don't actually use STL but the same idea applies) as a data member.  This class then provides a limited number of member functions that have just the interfaces I require of this class.  So there is less chance for code somewhere of accessing the STL class directly and altering it in ways that I didn't originally intend.  This would be considered a "thin wrapper".

>> My gut feel is that this ought to be one line in my .h
>> file, soemthing like:
>>
>>extern slist<ObjectClass> ObjectContainer;

Well what that is doing is defining an instance of the class, not a wrapper class.  So this is using the STL class directly.  That is fine too, its a matter of your needs and your tastes.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
it doesn't matter if he have difrent class's he can use STL class's anyway
he can use the containers idea using STL
or maximum use the STL class's and build one of his own



so i think you should use STL and except
abancroft's comment

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
ntdragon, I'm not understanding you there.
0
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
what i tried to say is that klopter can use STL in any case
if he want to use difrent class's he can use pointer's as his data
if he want's more option's then those in STL he can build his calss based on STL class's <i mean using STL class's>

0
 

Author Comment

by:klopter
Comment Utility
The upshot is that
  list<int> mylist;
gives me exactly what I need and I
don't have to initialize it to
null or delete it after I am through -
it will disappear when the context it
is used in does.

It all seems too easy.

Ken
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> It all seems too easy.
That is the idea.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now