Jump to content
  • Advertisement
Sign in to follow this  
Thomo

Linked lists and objects

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
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;

// ...
}



It could be done with references as well, but in this case they're slightly more ugly in my humble opinion. EDIT: Unless it's possible to have a std::list of references to objects?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!