Changing the world class from within another class

I have a world class that contains a list of objects. How do I add more of those objects to the list from the destructor of one of said objects? (The idea is that one breaks into multiple pieces)
Since your world class (this means a base class I guess) contains all thess objects I guess that means it contains the constructor of these objects. You can put inside the potentially destroyed object some heap allocated objects (that don't disappear with the destruction of this object).

1
2
3
4
5
6
7
Object1::~Object1() //this is supposed to be an object of that is going to be destroyed 
{
Object2 *object2 = new Object2(..); //this will be created with the destruction of an Object1
Object3 *object3 = new Object3(..); //this will be created with the destruction of an Object1
...
//add to your list of objects in world class depending on what container you use for that
}
Last edited on
That smells like a serious memory leak.
I don't know what this means
heap allocated objects

and my question is basically how to go about doing this
//add to your list of objects in world class depending on what container you use for that
I'm using a list container by the way.
blueeyedlion, I wouldn't worry about the code you see above. Seriously, I think it's going to cause more problems than it solves (which means at least 1 problem).
How so?
Why really
it's going to cause more trouble than it solves
?

As far as I know if you consider delete-ing your objects afterward no memory leak occurs!

If by the way you mean std::list you can add new elements by pushing back (or pushing front as well):
1
2
3
list<Base_Object *> myObjects;
Object1 *pObject1 =  new Object1;
myObjects.push_back (pObject1);


You must apply delete on each Object you no longer need though.
The problem with list is that you must iterate from a known position (normally the beginning or the end) to the desired position.

Edit: Base_Object explanation

By the way the way this approach requires that all your objects derive from a common base class so that all could be stored (through pointers) to a list of such objects.
Last edited on
Is it really necessary to use pointers with a list like that?
can't you do something equivalent to this?
1
2
3
list<int> myList;
int x = 5;
myList.push_back (int (x));

This compiles fine.

but back to my original question, how do I use this method to add to the list from the destructor?
Would it be a good idea to just give the object class a reference to the world class?
Last edited on
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.
As helpful as this is, my original question is left unanswered.
What you need is a link to your world from the object. It may look like something like this:
1
2
3
4
5
6
7
class World;
class Object
{
/* ... */
private:
World& master_; // link to world, need to be initialized in constructor
}

Then you can use master_ to access your world object into Object destructor. Althought you better do the task of splitting your objects outside the destructor, but it's my opinion.
I put master_ in reference so that you are forced to initialize it into the constructor. You can put a pointer instead if you want to initialize it after but don't forget to set it to NULL in the constructor.
That's exactly what I need!
but why the underscore after "master"?
Topic archived. No new replies allowed.