C++: How to build an events / messaging system without void pointers?

Posted by Jarx on Stack Overflow See other posts from Stack Overflow or by Jarx
Published on 2011-03-20T17:28:17Z Indexed on 2011/03/20 19:21 UTC
Read the original article Hit count: 342

Filed under:
|
|

I'd like to have a dynamic messaging system in my C++ project, one where there is a fixed list of existing events, events can be triggered anywhere during runtime, and where you can subscribe callback functions to certain events.

There should be an option for arguments passed around in those events. For example, one event might not need any arguments (EVENT_EXIT), and some may need multiple ones (EVENT_PLAYER_CHAT: Player object pointer, String with message)

The first option for making this possible is allowing to pass a void pointer as argument to the event manager when triggering an event, and receiving it in the callback function.

Although: I was told that void pointers are unsafe and I shouldn't use them.

  • How can I keep (semi) dynamic argument types and counts for my events whilst not using void pointers?

© Stack Overflow or respective owner

Related posts about c++

Related posts about events