Why do Listener callbacks have Broadcaster* not Broadcaster& arguments?

Could anyone kindly explain why Listener callbacks (e.g. Slider::Listener’s methods) take Broadcaster* arguments rather than Broadcaster& arguments?

Does this mean that it is not guaranteed that the Broadcaster outlives the Listener? If that is the case, when implementing the Listener callback functions, must I always check that the Broadcaster* points to a valid object before accessing into it (e.g. Slider->getValue()) and if so, how (given that it’s just a raw pointer)?

Broadcaster* broadcaster;
if( broadcaster != nullptr ) { }


Even that wouldn’t save you, because the raw pointer will remain pointing to that adress. If it was freed meanwhile, your (broadcaster!=nullptr) will still evaluate to true.
The concepts to make this safe are:

*WeakReference< ObjectType, ReferenceCountingType >
*std::weak_ptr< class T >

1 Like