I would like to create in C++ a Notifier class that I will use in other objects to notify various holders when the object gets destroyed.
template <class Owner> class Notifier<Owner> { public: Notifier(Owner* owner); ~Notifier(); // Notifies the owner that an object is destroyed }; class Owner; class Owned { public: Owned(Owner* owner); private: Notifier<Owner> _notifier; };
My point is that as I have a dense and complicated object graph, I’d like to avoid storing the address of the owned object in the notifier. Is there a way to change my notifier class so that it can deduce the owned object’s address from its own address and an offset that would be computed at compile time?
Note also that any object may have to notify several ‘owners’, possibly from the same class.
Thanks.
Or something like this :
Inherit from your notifier and add Owned as template parameter. Then you can have a owned method available inside the notifier :