Cloning C# Delegates in C++
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?
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.
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.
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.)
So I guess function pointers would also be an option.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement