Class Owner
owns multiple objects of class Item
via unique_ptr
. I want objects of Item
to store a reference (not a pointer) to the Owner
objects which owns them, with the following requirements:
There can never be a dangling reference.
The reference contained in an
itm
object of classItem
never points to anOwner
object which does not ownitm
.An
Item
object always has an owner.Item
class will be used via its subclasses.
I tried automatically registering the objects of Item
class during construction (in Item::Item()
), but it leads to double deletion when they're created on the stack. Hence, I probably need to prevent them being created on the stack. But that's a problem when I am subclassing them. Is there an easier way of ensuring that the mutual links are always correct, without relaxing any of the requirements? I suspect that 3. might have to be relaxed, to enable creating the object first and registering it with the Owner
later.