Very simple events implementation I've been thinking about implementing events and wrote some abstract code:

#include &lt;memory&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;

template&lt;typename Event&gt;
class Dispatcher
{
public:
    class Listener
    {
    public:
        virtual void OnEvent(Event&amp; event, Dispatcher&amp; sender) {};
    };

private:
    typedef std::shared_ptr&lt;Listener&gt; ListenerPtr;
    typedef std::vector&lt;ListenerPtr&gt; Listeners;
    Listeners _listeners;

public:
    void Reg(ListenerPtr listener)
    {
        if( _listeners.end() != std::find(_listeners.begin(), _listeners.end(), listener) )
            return;

        _listeners.push_back(listener);
    }

    void Unreg(ListenerPtr listener)
    {
        Listeners::iterator iter = std::find(_listeners.begin(), _listeners.end(), listener);
        if( _listeners.end() == iter )
            return;

        _listeners.erase(iter);
    }

    void Dispatch(Event&amp; event)
    {
        for( Listeners::iterator iter = _listeners.begin(); iter != _listeners.end(); ++iter )
            (*iter)-&gt;OnEvent(event, *this);
    }
};

struct SomeEvent
{
    int someParam;
    SomeEvent(int someParam) : someParam(someParam) {}
};

class SomeDispatcher : public Dispatcher&lt;SomeEvent&gt;
{
};

struct OtherEvent
{
    int otherParam;
    OtherEvent(int otherParam) : otherParam(otherParam) {}
};

class OtherDispatcher :
    public SomeDispatcher, public Dispatcher&lt;OtherEvent&gt;
{
};

class Consumer :
    public Dispatcher&lt;SomeEvent&gt;::Listener,
    public Dispatcher&lt;OtherEvent&gt;::Listener
{
    virtual void OnEvent(SomeEvent&amp; event, Dispatcher&lt;SomeEvent&gt;&amp; sender)
    {
        std::cout &lt;&lt; "OnEvent SomeEvent " &lt;&lt; event.someParam &lt;&lt; std::endl;
    }

    virtual void OnEvent(OtherEvent&amp; event, Dispatcher&lt;OtherEvent&gt;&amp; sender)
    {
        std::cout &lt;&lt; "OnEvent OtherEvent " &lt;&lt; event.otherParam &lt;&lt; std::endl;
    }
};


int main(int argc, char **argv)
{
    OtherDispatcher dispatcher;

    std::shared_ptr&lt;Consumer&gt; consumer(new Consumer());

    dispatcher.Dispatcher&lt;SomeEvent&gt;::Reg( consumer );
    dispatcher.Dispatcher&lt;OtherEvent&gt;::Reg( consumer );

    dispatcher.Dispatcher&lt;SomeEvent&gt;::Dispatch(SomeEvent(1));
    dispatcher.Dispatcher&lt;OtherEvent&gt;::Dispatch(OtherEvent(2));

    dispatcher.Dispatcher&lt;SomeEvent&gt;::Unreg( consumer );

    dispatcher.Dispatcher&lt;SomeEvent&gt;::Dispatch(SomeEvent(3));
    dispatcher.Dispatcher&lt;OtherEvent&gt;::Dispatch(OtherEvent(4));

    return 0;
}


It compiles and works as expected, but... I have to write more code to inherit from Dispatcher and Listener, to register listener and to define more exactly which dispatcher I'm registering to. And this method does not allow registering of custom class method, but only OnEvent().

Apart from this, are there any issues with this code? Is it compatible with different compilers and platforms? Any suggestions?
Listner needs a virtual destructor
and the OnEvent() method should probably be pure virtual:

class Listener
{
public:
    virtual void OnEvent(Event&amp; event, Dispatcher&amp; sender) {};
};

// Try

class Listener
{
    public:
        virtual ~Listener() {}
        virtual void OnEvent(Event&amp; event, Dispatcher&amp; sender) = 0;
};


You need the virtual destructor because delete will more than likely be called via a pointer to the base class (Listener) at some point.

No point in implementing OnEvent() to do nothing if you need sombody to over-ride it.

Personally I would prefer to use boost::ptr_vector&lt;&gt; rather than a std::vector&lt;std::shared_ptr&lt;&gt;&gt;. The boost vector has a couple of advantages (less lcoks required). Also its members are provided via references rather than pointers so it makes it more natural to use with the standard algorithms (not such a big deal in C++11 (with lambdas) but it makes C++03 much easier to use).

typedef std::shared_ptr&lt;Listener&gt; ListenerPtr;
typedef std::vector&lt;ListenerPtr&gt; Listeners;
Listeners _listeners;

// Replace with:

boost::ptr_vector&lt;Listener&gt;  listeners;


Be careful of using '_` on the front of identifiers. The rules are not obvious so best avoided to make sure you don't actually break them. Underscore Rules

Personally not a fan of the following:

   if( _listeners.end() != std::find(_listeners.begin(), _listeners.end(), listener) )


Nothing wrong with it. But reversing the order of the test just makes you pause a second. See Yoda Conditionals.

Prefer to use standard algorithms rather than hand coded loops.

   for( Listeners::iterator iter = _listeners.begin(); iter != _listeners.end(); ++iter )
        (*iter)-&gt;OnEvent(event, *this);

   // Try
   std::for_each(listeners.begin(), listeners.end(),
                 std::bind2nd(Listner::OnEvent, event)
                ); // Assume boost::ptr_vector&lt;&gt;

   // Or go all C++11 on us
   std::for_each(std::begin(listeners), std::end(listeners),
                 [&amp;event](std::shared_ptr&lt;Listener&gt;&amp; lis) {lis-&gt;OnEvent(event);}
                );


If you want to have listeners that only deal with a specific type of event. You can write your Listner to be a filtering lsitener.

class Listener
{
public:
    virtual void OnEvent(Event&amp; event, Dispatcher&amp; sender) {};
};


Change to:

template&lt;class ForEvent&gt;
class Listener
{
    public:
        ~Listener() {}

        // This is the method called by the dispatcher.
        void eventFilter(OnEvent&amp; event, Dispatcher&amp; sender)
        {
             // dynamic_cast to a pointer type.
             // Result is NULL if not the correct type.
             // If you used a value type then it would throw an exception.
             ForEvent* specialEvent = dynamic_cast&lt;ForEvent*&gt;(&amp;event);
             if (specialEvent != NULL)
             {
                 // Only call the OnEvent for the types we want.
                 // put back to a references (so OnEvent does not need to
                 // check for NULL).
                 this-&gt;OnEvent(*specialEvent, sender);
             }
        }

        virtual void OnEvent(ForEvent&amp; event, Dispatcher&amp; sender) = 0;
};