Cloning C# Delegates in C++

Started by
4 comments, last by Cygon 15 years, 5 months ago
Hello, Has anyone tried to clone C# style delegates in C++? Delegates get expanded to classes in C#, so it seems like it should be possible to implement in C++. Any sample code to share?
-----Quat
Advertisement
Boost.Signals

Possibly the closest thing you're going to get. Beware, though, I've heard that it's horrendously and rediculously slow. If you're planning on using it in a time-critical loop or have a large system based on it, you may need to profile its performance characteristics to see if it is suitable for your purposes. In most cases I'd say it'd be fine, though.
NextWar: The Quest for Earth available now for Windows Phone 7.
Class Delegate {
virtual void operator ()() = 0;
};

Now anything that inherits from Delegate is a delegate(type safe and all that jazz). Though it will get annoying to have to write the base class for every function signature you want.

From MSDN on Delegates(I had to look it up I haven't used them before.)
Quote:
Delegates have the following properties:

*Delegates are similar to C++ function pointers, but are type safe.


So I guess function pointers would also be an option.
Quote:Original post by Sc4Freak
Boost.Signals

Possibly the closest thing you're going to get. Beware, though, I've heard that it's horrendously and rediculously slow. If you're planning on using it in a time-critical loop or have a large system based on it, you may need to profile its performance characteristics to see if it is suitable for your purposes. In most cases I'd say it'd be fine, though.


boost::function and boost::bind also come pretty close.
The fastdelegate comes pretty close IMHO:

Clicky at CodeProject

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

I'm using libsigc++ in most of my projects (together with Boost, but I prefer libsigc++ for delegates and events).

Another option is the library written by Sneftel here on gamedev.net which focuses on performance: Very, very fast event library.

If not for the library, that thread is interesting as well for the discussion about different library implementations providing signaling and callback services and for the links provided to benchmarks of those libraries.
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

This topic is closed to new replies.

Advertisement