Click here to Skip to main content
14,846,874 members
Articles / Programming Languages / C++
Posted 24 Feb 2004


43 bookmarked

Emulating C# delegates in Standard C++

Rate me:
Please Sign up or sign in to vote.
4.55/5 (7 votes)
24 Feb 20045 min read
Yet another way create C#-style delegates in C++ using a mixture of templates and polymorphism


C# has a language feature called 'delegates' which makes it easy to detach the originator of an event from the ultimate handler. They perform essentially the same role as function pointers in C and pointers to member functions in C++, but they are much more flexible. In particular they can be used to point to any function on any object, as long as it has the appropriate signature.

This article explains my approach for providing the same functionality using only standard C++. There are many worthy alternatives which you can find easily by Googling with "C++ delegates". The focus of my effort was to obtain a syntax very similar to that in Managed C++ and C#.


If you already know everything you want to know about delegates, please skip this section.

Delegates are not a new idea. Borland's Delphi and C++ Builder products have used them from the outset to support the Visual Component Library, though they are called 'method pointers' in Delphi, and 'closures' in Builder (they are the same thing as far as I know). Basically a closure is an OO function pointer. Internally it simply holds the address of the function to be called plus the address of the object on which it is being called (i.e. the hidden 'this' parameter which is passed to the function).

The important point is this: being agnostic about which other class is going to handle the events a class generates is a key factor in making Delphi's wonderful component-based visual development framework possible. Reduced coupling between classes is a Good Thing.

Now C#-style delegates provide the same service to .NET languages but, sadly, Standard C++ does not have them. Pointers to member functions are very restricted in comparison, though they have been used in conjunction with macros in libraries such as Borland's OWL in the past, and (I think) with Trolltech's Qt today. And that's why I'm writing this article.

With the .NET delegates, you can even attach several handlers to a single event. They will all be called (sequentially) when the delegate is invoked. This means you can safely attach your handler to an event without breaking someone else's connection. I haven't used this feature yet, but I recognize the potential. However, I do question whether such a feature can be implemented efficiently. Invoking Borland's single-cast closures boils down to a couple of opcodes which push 'this' and call the function, so they are powerful but still cheap to use. [It's a pity they're not in the Standard...] But once you start maintaining a dynamic collection of targets, life gets more complicated. Ideally, .NET should have really efficient single-cast delegates, and implement multicast delegates in terms of those - best of both worlds. For all I know, that's what it does.

.NET distinguishes between 'delegates' and 'events'. A delegate is a glorified function pointer, as I have said; an event is an application of a delegate - a member of a class to which you can assign the addresses of handlers which will be called when the class invokes the delegate. I admit to finding the distinction unhelpful - how else would delegates be used, anyway? I sometimes use the terms interchangeably.

C# and Managed C++ have a rather tidy syntax for assigning handlers to a delegate/event:

mnuPopup->Popup += new System::EventHandler(this, mnuPopup_Popup);

When the context menu, mnuPopup, is displayed by the user, it will invoke its Popup event/delegate. This will then call the handler I have implemented in my form, mnuPopup_Popup. I have tried to preserve something like this syntax in my code.

Understanding the code

Enough waffling!! Let's talk about the code. If you couldn't care less about how it works, just skip this section and head straight for 'Using the code'

The design is intended to satisfy the following constraints:

  • The delegate can call non-static member functions on an object of any class, including virtual functions.
  • The delegate can call static member functions of any class, and free functions.
  • The delegate is multicast.
  • The delegate is easy to use (i.e. keeps close to the C# syntax).

There are four parts to my solution:

  1. A base class to specify signature without reference to any actual classes. It has a pure virtual overload of operator(). This is a private nested class.
  2. An outer class actually used by clients. It invokes operator() on a collection of derivatives of the base class.
  3. Derived classes which implement the overloaded operator() declared in the base class. This is a public nested template class, and allows us to specify the type of an event target.
  4. A derived class to handle static and free functions. This is a public nested class.

The base class looks like this (Return and Arg1 are types from the outer class):

  1  class Base
  2  {
  3    public:
  4      virtual ~Base() { }
  5      virtual Return operator()(Arg1) = 0;
  6  };

The derived class for non-static functions looks like this (Return and Arg1 are types from the outer class):

  1  template <typename Class>
  2  class T : public Base
  3  {
  4    // Signature applied to a pointer-to-member for target class.
  5    typedef Return (Class::*Func)(Arg1);
  7    private:
  8      Class* mThis; // Pointer to the object we are delegating to.
  9      Func   mFunc; // Address of the function on the delegate object.
 11    public:
 12      T(Class* aThis, Func aFunc) : mThis(aThis), mFunc(aFunc) { }
 14      virtual Return operator()(Arg1 arg1)
 15      {
 16        return (mThis->*mFunc)(arg1);
 17      }
 18  };

The derived class for static and free functions looks like this (Return and Arg1 are types from the outer class):

  1  class S : public Base
  2  {
  3    typedef Return (*Func)(Arg1);
  5    private:
  6      Func mFunc; 
  8    public:
  9      S(Func aFunc) : mFunc(aFunc) { }
 11      virtual Return operator()(Arg1 arg1)
 12      {
 13        return mFunc(arg1);
 14      }
 15  };

The outer class looks like this (with many details missing):

  1  template <typename Return, typename Arg1>
  2  class Event 
  3  {
  4    private:
  5      std::vector<Base*> mPtrs;
  6      class Base { ... };
  8    public: 
  9      template <typename Class>
 10      class T : public Base { ... }; // Non-static
 11      class S : public Base { ... }; // Static
 13      // Add a new target (callee) to our list.
 14      Event& operator+=(Base* aPtr)
 15      {
 16        mPtrs.push_back(aPtr);
 17        return *this;
 18      }
 20      // Call all the targets - there will be horrible undefined behaviour
 21      // if the callee object no longer exists. You have been warned!
 22      Return operator()(Arg1 arg1)
 23      {
 24        // There are problems here:
 25        // 1. Which result should the multicast return? 
 26        // For now I say the last called item.
 27        // 2. We need to can't store a temporary when Return is void.
 28        typename std::vector<Base*>::iterator end = mPtrs.end();
 29        for (typename std::vector<Base*>::iterator i = mPtrs.begin();
 30          i != end; ++i)
 31        {
 32          // Probably a specialisation for Return == void would be better.
 33          if ((i + 1) == end)
 34            return (*(*i))(arg1);
 35          else
 36            (*(*i))(arg1); 
 37        }
 38      }
 39  };

There is still some work to do. I want to make it safe to copy these objects, and I need to come up with something for multicast delegates with a signature that returns are value. Perhaps I could return a vector of results. I also need to duplicate the template to cope with two or more arguments to the signature.

No doubt this implementation is very slow compared to function pointers but the usual application of events is GUI work, so speed is not so critical. I'd be very interested to see what the underlying implementation of .NET delegates and events looks like...

It seems that my Event class would be most useful as a public member of any class wishing to expose an event. This breaks encapsulation, but I think the syntax for adding targets would get pretty hairy otherwise. We want to prevent clients of a class using Event from invoking operator() on it. Perhaps a simple adapter would do the job - it would be a public member passing calls on to a private Event member, but not exposing operator().

Using the code

To try this out, download the demo code. Everything is contained in a single file to make life easy. Just compile it and run it from the command prompt. This code was developed on g++ 3.2. I'm keen to know which other compilers like it, and which ones don't, so please let me know.

  1  struct TShapes
  2  {
  3    virtual void Square(int i)   
  4      { std::cout << "TShapes::Square:          " << i << std::endl; }   
  5    void Triangle(int i)         
  6      { std::cout << "TShapes::Triangle:        " << i << std::endl; }   
  7  };
  9  struct TDerivedShapes : TShapes
 10  {
 11    virtual void Square(int i)   
 12      { std::cout << "TDerivedShapes::Square:   " << i << std::endl; }  
 13  };
 15  struct TThings
 16  {
 17    void Thing1(int i)           
 18      { std::cout << "TThings::Thing1:          " << i << std::endl; }   
 19    static void Thing2(int i)    
 20      { std::cout << "TThings::Thing2:          " << i << std::endl; }   
 21  };
 23  void Free(int i)               
 24      { std::cout << "Free:                     " << i << std::endl; } 
 26  int main()
 27  {
 28    // As usual, a typedef makes life easier when using templates.
 29    typedef Event<void, int>  MyEvent; 
 30    MyEvent event;
 32    TShapes        shapes;
 33    TDerivedShapes shapes2;
 34    TThings        things;  
 36    // These items get the ball roling.
 37    event += new MyEvent::T<TShapes>(&shapes, &TShapes::Square);
 38    event += new MyEvent::T<TShapes>(&shapes, &TShapes::Triangle);
 40    // This item shows that virtual functions are handled correctly.
 41    event += new MyEvent::T<TShapes>((TShapes*)&shapes2, &TShapes::Square);
 43    // This item shows that inherited functions are handled correctly.
 44    event += new MyEvent::T<TDerivedShapes>(
 45      &shapes2, &TDerivedShapes::Triangle);
 47    // This item shows that the Event object 
 48    // can hold a truly heterogeneous 
 49    // collection of targets.
 50    event += new MyEvent::T<TThings>(&things, &TThings::Thing1);
 52    // This item shows that static functions are handled correctly.
 53    event += new MyEvent::S(&TThings::Thing2);
 55    // This item shows that free functions are handled correctly.
 56    event += new MyEvent::S(&Free);
 58    // Invoke the multicast event 
 59    std::cout << "<multicast>" << std::endl; 
 60    event(100);  
 61    std::cout << "</multicast>" << std::endl;
 63    return 0; 
 64  }

This is what you should see when you run the program:

  1  <multicast>
  2  TShapes::Square:          100
  3  TShapes::Triangle:        100
  4  TDerivedShapes::Square:   100
  5  TShapes::Triangle:        100
  6  TThings::Thing1:          100
  7  TThings::Thing2:          100
  8  Free:                     100
  9  </multicast>


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Arnold the Aardvark
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

Generaloverride the -= Pin
delaneyj30-May-06 12:42
Memberdelaneyj30-May-06 12:42 
Generalcompilation error : error C2562: '()' : 'void' function returning a value Pin
sandip.ware7-Jun-05 21:29
Membersandip.ware7-Jun-05 21:29 
compilation error in VC++6.0

c:\download\c++\codes\standard_cpp_delegates_demo\standard_cpp_delegates_demo.cpp(116) : error C2562: '()' : 'void' function returning a value

There is compilation error in downloaded code
return (*(*i))(arg1); //Error code. compilation error

GeneralI have a *very* efficient implementation of closures, if you're interested Pin
Don Clugston2-Mar-04 12:35
MemberDon Clugston2-Mar-04 12:35 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Philippe Bouteleux3-Mar-04 8:12
MemberPhilippe Bouteleux3-Mar-04 8:12 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Arnold the Aardvark3-Mar-04 9:08
MemberArnold the Aardvark3-Mar-04 9:08 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Don Clugston3-Mar-04 14:15
MemberDon Clugston3-Mar-04 14:15 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Arnold the Aardvark4-Mar-04 9:12
MemberArnold the Aardvark4-Mar-04 9:12 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Don Clugston6-Jul-04 15:07
MemberDon Clugston6-Jul-04 15:07 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
SecondAct29-Nov-05 15:03
MemberSecondAct29-Nov-05 15:03 
GeneralRe: I have a *very* efficient implementation of closures, if you're interested Pin
Don Clugston29-Nov-05 23:11
MemberDon Clugston29-Nov-05 23:11 
Generalprevious articles Pin
Philippe Bouteleux2-Mar-04 10:20
MemberPhilippe Bouteleux2-Mar-04 10:20 
GeneralRe: previous articles Pin
Corneliu Tusnea2-Mar-04 14:30
MemberCorneliu Tusnea2-Mar-04 14:30 
GeneralRe: previous articles Pin
Arnold the Aardvark3-Mar-04 9:20
MemberArnold the Aardvark3-Mar-04 9:20 
GeneralComments Pin
mortal27-Feb-04 2:58
Membermortal27-Feb-04 2:58 
GeneralRe: Comments Pin
Arnold the Aardvark27-Feb-04 11:22
MemberArnold the Aardvark27-Feb-04 11:22 
GeneralCompiler check Pin
mortal27-Feb-04 2:51
Membermortal27-Feb-04 2:51 
GeneralRe: Compiler check Pin
Don Clugston2-Mar-04 12:47
MemberDon Clugston2-Mar-04 12:47 
GeneralRe: Compiler check Pin
Arnold the Aardvark3-Mar-04 9:03
MemberArnold the Aardvark3-Mar-04 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.