Can I use events in C++ like in C#?

Started by
13 comments, last by wqking 12 years, 1 month ago

If you're using Visual Studio 2010 or newer (or a compiler that supports C++0x);


bool CGame::Create( /* */ )
{
tr1::function<void (CGame*)> CGameOnLoading = &CGame::OnLoading;
m_nState = EGS_WORLD;//EGS_LOADING;
CGameOnLoading( this );

return true;
}


Note that the tr1 namespace is inside the std namespace.


TR1 is not necessary. VS2010 has std::function support as part of its C++11 support. Its also important to note that std::function is not the same as boost signals and slots, which allows for registration and deregistration of multiple listeners.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement

TR1 is not necessary. VS2010 has std::function support as part of its C++11 support. Its also important to note that std::function is not the same as boost signals and slots, which allows for registration and deregistration of multiple listeners.


I could of sworn MSDN said it was apart of TR1, but I may be mistaken since I read it up around 2/3ish AM. +1 :)
As much as I dislike using libraries, I may try giving Boost a shot, your last sentence really has got me interested.

I could of sworn MSDN said it was apart of TR1

TR1, or "Technical Review 1", is part of the work leading up to (and predating) C++11. What Washu is getting at is that the technical reviews are not part of the C++ standard itself, although what they contained was effectively rolled into the core C++ standard in C++11 (with various errata to e.g. take advantage of new language-level features, and little things like putting them in the root std namespace, etc).
If you want to use something that works without a hassle, try fastdelegate http://www.codeproje...astest-Possible It's only a header to include.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Beside Boost::Signal and Boost::Signal2, there is also libsigc++ you may want to try.
Just a reminder if you use callbacks heavily You'd better benchmark, or investigate, between Boost and libsigc++ to see the performance.
As far as I know and some google result, libsigc++ has better performance.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement