"Joseph Turian" <tu****@gmail.com> schrieb im Newsbeitrag
news:11*********************@g14g2000cwa.googlegro ups.com...
Okay.
So I have a class A, which contains a vector of B.
Any B item is owned by a single, unique A. Indeed, when we create A, A
creates its B items.
B would like to know which A it is contained in, without having to be
perpetually told it by being passed A when B methods are called.
How can B store its A (or some handle to A) if A stores B?
Say that B has a pointer to its A.
However, if I create a copy of an A object and delete the original, all
the B->A pointers are invalidated. Doh!
If an object knows that it is owned by another object you shouldn't delete
it without telling its owner. How would you like it, if the money you own is
spent by someone else without telling you?
Also, if an owned object is copied and the new object should also be owned,
the owner of the new object must learn about the existance of the new
object. Otherwise you must not copy the pointer (or whatever) to the owner.
If your objects are so closely related, the owning object must be notified
of all relevant changes of an owned object, and of cause the owning object
must also inform its owned objects of all relevant changes.
It might be usefull in some cases, that an owned object knows about its
owner. But even then an owned object should not implement operations, which
create or delete owned objects. You could provide some method to get the
owner of an object and then call methods of the owning object to create or
remove owned objects. You can even provide such methods for owned objects,
but such methods should only call methods of the owning objects to perform
such tasks.
HTH
Heinz