Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is the simplest cleanest way to implement a container?

Posted on 2000-04-06
10
Medium Priority
?
183 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

722 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