The way I see it it is necessary to use them because otherwise you must specify the number of objects that it's going to be created at compile time. If you know them then it's really enough to just create and use the object your way. On the other hand if you need to add some unknown number of objects during compilation there isn't a lot of ways.
I haven't though your problem thoroughly but I think that you must find a way to access your objects in your list. I am not sure a list is the best container for that. Imagine a list that contains 5 objects. You need to delete an object assigned to the 3rd slot. You must know at that point that THIS object is at 3rd slot. But how do you know that? If you only use
push_back
and
erase
then it's easy to lost control of your elements.
I was thinking maybe another container is better like vector for instance. If you intend to use a lot of insertion at random places and deletion it's slower than list but you have random access to your elements. Maybe you should keep a track of your element creation.
One suggestion might be (I am not saying it's the best or easier one):
1) create a predefined size
vector
of your expected object numbers.
1 2
|
vector<Object *> myObjects;
myObjects.resize(N, 0); //N is this number of objects
|
2) You can start assigning objects inside that vector as long as there is space (less that N objects are assigned). If not push_back every new object.
3) At the same time you can keep track of your objects by their index. You can add this to your derived classes (that represent your objects) as a id number or something. This could be an
int
(presuming that not a huge number of objects is going to be created). Use this number to specify which object should be deleted inside vector.
Anyway this is all theoretical suggestion. If you start implementing this (or another approach) then you may find some other things arising.