Jump to content
  • Advertisement
Sign in to follow this  
Quat

Cloning C# Delegates in C++

This topic is 3530 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!