开发者

How do you implement Signals and Events in C++?

开发者 https://www.devze.com 2023-03-20 00:58 出处:网络
I am making a Gui Library on top of SDL using C++. (Don\'t ask me why, I am just doing it to gain some knowledge out of practice in order to understand how Gui libraries are made.) And I want to make

I am making a Gui Library on top of SDL using C++. (Don't ask me why, I am just doing it to gain some knowledge out of practice in order to understand how Gui libraries are made.) And I want to make a signal connection system like gtk+ or wxWidgets...

 g_signal_connect(mybutton,"clicked",gtk_main_quit); //Gtk+

 EVT_MENU(wxID_EXIT,OnQuit); //WxWidgets

I understand I can do this using function pointers. But how do I add the functions to the main loop?

Or开发者_开发问答 Is there a better way to do this?


The simplest way is borrowing it from a library, for example boost::signal or boost::signal2. The next best thing is implementing your own but borrowing most of the features from libraries like boost::bind (to enable the connections) or using C++0x features that will simplify the generic connection of clients (std::function / boost::function).

I would recommend that you use boost and their signals2 library, as there are many things that you can do wrong and are already solved there.

0

精彩评论

暂无评论...
验证码 换一张
取 消