• ### Popular Now

• 12
• 12
• 9
• 10
• 13

This topic is 4066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I create on obect during runtime. This is added to a linked list. Object A. From Object A, Object Aa is created. (They are not the same object). Object Aa's position is queried for position in 3D world. Object A will be destroyed at some point. How do I tell ObjectAa to also get destroyed. Object Aa is a child of A, but hey are in different linked lists. I thought about using an ID, but there positions in the list may not be the smae. Can some advise me please.

##### Share on other sites
If A creates Aa, then A should have a pointer to Aa. In A's distructor then you free up Aa.

theTroll

##### Share on other sites
If A creates Aa, then A should have a pointer to Aa. In A's distructor then you free up Aa.

theTroll

##### Share on other sites
If A creates Aa, then A should have a pointer to Aa. In A's distructor then you free up Aa.

theTroll

##### Share on other sites
Sorry, I worded my question badly.

Aa is not created from A. It is created separately, but is dependent on A.

When A gets deleted, I want Aa to exist still (for a short time). Hence I cannot make Aa part of A otherwise it (Aa) will get deleted with A.

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
"Object A will be destroyed at some point. How do I tell ObjectAa to also get destroyed"

"Aa is not created from A. It is created separately, but is dependent on A.

When A gets deleted, I want Aa to exist still (for a short time). Hence I cannot make Aa part of A otherwise it (Aa) will get deleted with A."

Ok, so there are some incompatible ideas here ... but it seems to me you are describing a system where:

1. Object A does not in any way depend upon the existance of or need to know about (for its own purposes) Object Aa. Object Aa in some way depends upon Object A, but not for its sheer existance, just for functionality of some sort? And perhaps will decide to die when Object A no longer exists?

This is an Observer type situation.

Object A could have a list of "dependents" that it "notifies" about its relevant events, such as impending deletion. Object Aa could register whatever function (with matching signature of course) it wants to be called by object A's deletion ... of course if Object Aa could exist before Object A, then the question becomes, how does it get told of Object A's creation? This duty can of course only fall to some 3rd party controller object, since if that is the case, either A or Aa can each be independently created and yet still need to interact.

class Aa{    // ...};class A{    A(Aa*);    private:        Aa *myAa;    };A::A(Aa *MyAa) : myAa(MyAa){ }typedef std::list<*Aa> listAa;typedef std::list<A> listA;int main(){    listAa myAas;    listA myAs;    Aa *someAa = new someAa;    myAas.push_back(someAa);    myAs.push_back(A(someAa));    // ...    /* Because it's a list of pointers, the list won't clean up the memory       that we allocated ourselves, so we have to do that before clearing       the list or we'll leak memory. */    for (listAa::iterator it = myAas.begin(); it != myAas.end(); ++it)        delete *it;    // ...}