Raise an event and begin listening to it

Hello,

any simple way to raise an event and set a listener for such event?

I used to do this in actionscript with addEventListener and removeEventListener...

creating custom events and so on...

How to do this in c++?

thank you
Create event library yourself or find one online.
Quick example of such system: http://coliru.stacked-crooked.com/a/1b9ec7059a4ff5f9
Last edited on
Thank you fort the example. May i further ask you if is possible to have a version without the boost library?

I haven't tried boost yet, since i read that installing and configuring for mingw and codeblock is a pain.
Here is an example using unions. Notice that we lost ability to use non-trivial types in events. http://coliru.stacked-crooked.com/a/51a94757c12a7d2f
It will be better to actually have type-safe templated event handling mechanism, but I do not have time for it right now.

I haven't tried boost yet
I strongly suggest to try. It is really useful library.
i read that installing and configuring for mingw and codeblock is a pain.
Unless you want to use parts which are need to be built beforehand (this page has list of them: http://www.boost.org/doc/libs/1_59_0/more/getting_started/windows.html#header-only-libraries), it is just header-only library. Which means that you need to unpack your boost package somewhere and and following line to the Settings → Compiler → Compiler settings → Other options
-isystem <Path to boost>
Where path to boost is path to the boost package (ending with directory like \boost_1_54_0)
You need -isystem to include boost libraries as system ones. It is done to suppress warnings in boost headers because they can break C::B.
As for the example using unions, the coliru page say: clang++ -std=c++14


I'm using c++11. There are c++14 function in the file?

No there is not.
> any simple way to raise an event and set a listener for such event?

Consider using boost::signals2 for this. (Note: it is not a 'header-only' library.)

http://www.boost.org/doc/libs/1_59_0/doc/html/signals2/tutorial.html
(For simple use of this kind, you won't need anything more than what is in the Beginner sections.)
Topic archived. No new replies allowed.