I discovered delegates while working in C#, which supports them natively. Unfortunately, C++ doesn't handle them so elegantly.
Basically, sometimes it would be nice to "subscribe" to certain events or messages in a system. For example, it might be nice to get a callback when a particular object is destroyed so you can clean up any references you have to it. I ran into this particular problem when I had an "enemy" object holding a pointer to the "player" object - this caused a crash when the player died and their object was destroyed.
There are ways to handle this, though most of them are inelegant or require the object generating the event to know more about the receiver than we would like (say, a common base class that the receivers can derive from). This is also fairly straightforward if you just want to call a static function, but what if you want to call a member function?
The main problem is that C++ just really doesn't let you easily
create a collection of completely unrelated types, and pointers to
member functions in unrelated classes are basically completely
C# handles this quite nicely by declaring a delegate - basically, a prototype for the callback function - and storing references to the object/function in a list. When you "call" the list of functions, every object/function in the list receives the callback with the arguments you provide.
There are a lot of attempts to do this in C++. C++11 may improve things. I just went and rolled my own, taking ideas from here and there and trying to keep the code as simple as possible. It involves a few macros to wrap some complexity, but seems to work.
The header file that makes this all work is down at the bottom. First, how it's used.
In the calling class, you need declare the event handler, specifying what gets passed to the receiver:
Note that my implementation only supports receiver functions that have a single argument - though if you need to pass more, store them in a class and construct an instance of it to pass to the event handler.
Then you need to declare the target function that matches the sender (argument type, receiving class name, and receiving function):
And then you need to add the target function to the sender:
Then in the sender, you can call NotifyDestroy(obj) with the object that was destroyed, and every class that added their event handler will get the callback.
Internally, the event handler works by keeping a list of static function pointers, and a void * to store the target class "this" pointer. The static functions are created by the EVENT_FN macro, and just redirect to the correct member function. These have to be static functions, because despite coming from different classes, the C++ compiler considers them to have the same signature and you can store a collection of them easily.
It seems like a bit of a hack, but this sort of redirection-to-class is done all the time in other callback situations - when you spawn a new thread for instance.
The header file that makes all of this work is below (Disclaimer: None of the code here has been properly tested. I modified this code to use STL instead of my own containers.)