Solved

What is the simplest cleanest way to implement a container?

Posted on 2000-04-06
10
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 4

Accepted Solution

by:
abancroft earned 50 total points
ID: 2690857
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
ID: 2690861
4. Do you know how many objects you'll be storing before you start adding them?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2690868
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

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

Expert Comment

by:nietod
ID: 2690894
>> 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
 
LVL 1

Expert Comment

by:ntdragon
ID: 2691391
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
ID: 2691428
ntdragon, I'm not understanding you there.
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2692446
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
ID: 2697742
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
ID: 2697833
>> It all seems too easy.
That is the idea.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 125
Issues with C++ Class 19 120
c++, dynamic object by json 1 65
draw a Christmas tree by using a nested loop? 26 105
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

751 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