Sign in to follow this  

Cloning C# Delegates in C++

This topic is 3317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this