I'm trying to design a simple event system that, "basically", looks like this:
HandleEvent(..) with different overloads for compile time type detection, instead of using dynamic_casting.What would be better to choose, when firing events: creating them on the stack and passing them by reference, or allocating them dynamically on the heap and use dynamic_casting and let the observer deallocate them when they're processed by the objects that can handle them? (e.g. isn't dynamic allocation unnecessary when an event can be fired quite often; what about dynamic casting, isn't it avoidable?).
Also, this is not quite a thread-safe scenario..
Do you need dynamic allocation? No. Typically, you want
void fireEvent()
{
Event ev;
for ( each observer )
observer.trigger(ev);
}
And the observer's signature
void trigger(const Event& ev);
Note that "passing references to them" isn't true, pedantically speaking. It's actually "passing them by reference".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With