Suppose you have to classes A & B whose objects have unrelated lifetimes:
class A
{
public:
IStateChanger& GetStateChanger() { return mStateChanger; }
private:
StateChanger mStateChanger;
};
class B
{
public:
void Register(IStateChanger& iStateChanger)
{
mpStateChanger = &iStateChanger;
}
void Notify()
{
mpStateChanger->DoSomething();
}
private:
IStateChanger* mpStateChanger;
};
At one point, I register a StateChanger, which I get from an A object, at an object of B. Now A dies, destroying its own StateChanger. But B lives and calls a method on the StateChanger, because it thinks it's still alive. My goal is to make the Notify() method as robust as possible.
Of course, we could write some automatic Register/Unregister with observers. This seems a lot of overhead to me.
Or pass a weak_ptr, which implies creating the StateChanger as a shared_ptr at the IA object. And, I need to have an extra method for returning the weak_ptr at IA instead of the reference, which is ugly when someone is using A directly.
Or, I could write a comment on how to use the API correctly and that it's evil to kill IA without unregistering the StateChanger at B. This will definitely lead to a crash.
From what I read, observer_ptr could be a choice - but its not standardized yet.
So here's my question: Am I thinking in the wrong direction? Is there a much simpler design?