Click here to Skip to main content
12,813,994 members (35,131 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 29 Mar 2007

An event mechanism for C++ using an interface based delegate

, 29 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Introducing a simple event mechanism for C++ using a C# style delegate.


This article introduces a very simple and comfortable event mechanism in C++. In many cases, a delegate in C++ is hard to understand because of its complexity. The attached code is a sample that uses the combination of the C# delegation style and Java's inner class callback style. It can show you an example of an easy event mechanism.

Event Processing

// delegate
struct ISampleEventHandler : public IEventHandler
    virtual void callback( IUnknown* object, const SampleEventArgs& e ) = 0;
    typedef SampleEventArgs _EventArgs; // passive polymorphism using typedef

// event
template<typename T>
class Event
    void invoke(IUnknown* object, const EventArgs& e = EventArgs::Empty)
        std::set<T*>::iterator iter;
        for (iter = m_EventHandler.begin();
            iter != m_EventHandler.end();
            // this code can promise the type-safe callback because
            // of the polymorphism using typedef in delegate
            (*iter)->callback(object, static_cast<const T::_EventArgs&>(e));
    void operator+=(T* eventHandler)

    void operator-=(T* eventHandler)

    std::set<T*> m_EventHandler;

Using the Code

We need an abstract parent class to identify the event sender.

class IUnknown
 virtual ~IUnknown() {}
 virtual std::string name() = 0;

Here is the event sending class:

class Callee : public IUnknown
// declare the event.
 Event<ISampleEventHandler> SampleEvent;
 void test()
  SampleEventArgs e;
  e.sampleData = 10;
  e.sampleData2 = 20;

// event occur.
  SampleEvent.invoke(this, e);
 std::string name() { return "callee"; }

Here is the event receiving class:

class Caller : public IUnknown
// register Caller's inner class object to Callee.
  m_Callee.SampleEvent += &sampleEventHandler;
// unregister Caller's inner class object from Callee.
  m_Callee.SampleEvent -= &sampleEventHandler;
 void test()
 void print(IUnknown* object, const SampleEventArgs& e)
  std::cout << object->name().c_str() << std::endl;
  std::cout << e.sampleData << std::endl;
  std::cout << e.sampleData2 << std::endl;
 std::string name() { return "caller"; }
 class SampleEventHandler : public ISampleEventHandler
  void callback( IUnknown* object, const SampleEventArgs& e )
    // to access outer class's member function.
    Caller* outer = reinterpret_cast<Caller*>((char*)this - 
                       offsetof(Caller, sampleEventHandler));

    outer->print(object, e);
 } sampleEventHandler;
 Callee m_Callee;

Points of Interest

You can easily find the reason why using the delegate is nice. It can break the cyclic dependency of each class - the 'Caller' depends on the 'Callee' but the 'Callee' does not have dependency with any classes.


  • 2007. 03. 29 - Initially released.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Korea (Republic of) Korea (Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

Questionstd::set ?!? Pin
emilio_grv31-Mar-07 4:28
memberemilio_grv31-Mar-07 4:28 
AnswerRe: std::set ?!? Pin
choiday31-Mar-07 6:25
memberchoiday31-Mar-07 6:25 
GeneralSome Suggestions Pin
Stuart Dootson30-Mar-07 0:13
memberStuart Dootson30-Mar-07 0:13 
GeneralRe: Some Suggestions Pin
choiday30-Mar-07 15:57
memberchoiday30-Mar-07 15:57 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Mar 2007
Article Copyright 2007 by choiday
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid