I am writing a simple event handling class in C++, to avoid having delegate registration and calls cluttering the rest of the code. I have a design that seems to do the job, but it has a couple of drawbacks.So far it looks like this:template<typename T>struct IDelegate { virtual void operator ()(const T & args) const = 0; };template<typename T>class EventHandler{public:    void Trigger(const T & args) const;    void AddListener(const IDelegate<T> & delegate);    void RemoveListener(const IDelegate<T> & delegate);};Which in practice would look like this (some code elided for clarity):class Foo{public:    struct CustomEventArgs { int i; };    EventHandler<CustomEventArgs> * SomeHandler() const { return m_handler; }    void SomeMethod()    {        // Some code        CustomEventArgs someArgs = { 42 };        SomeHandler()->Trigger(someArgs);    }private:    EventHandler<CustomEventArgs> * m_handler;};class Bar : public IDelegate<Foo::CustomEventArgs>{public:    void operator ()(const Foo::CustomEventArgs & args) const    {        std::cout << args.i <<  bottles of beer << std::endl;    }    void StartListening(const Foo & foo)    {        foo.SomeHandler()->AddListener(*this);    }};This code would basically do what I want, but there are still several things I don't quite like.EventHandler::Trigger() is publicAnyone can call it, which is a serious flaw. Only the owner should be able to call that method. I can see two ways of ensuring this, but I am not happy with any of them:Making the EventHandler(s) hidden from outside Foo. This defeats the purpose though, since it would then require to add as many AddListener/RemoveListener methods.Making Trigger() protected, deriving from EventHandler, then declaring Foo as a friend. This would require to declare a specific class for every single pair of event-handler/class-using-that-event-handler, which is less than ideal.SomeHandler() returns a pointerIf possible, I think it'd be better to make m_handler an actual member instead of having it somewhere (else) on the heap, and return a reference to it. But I don;t see how to do that without getting plagued with non-const-ness: I want StartListening() to take a const reference.The question[s]What would you recommend regarding these two issues?Do you see other flaws in this design?
Designing an EventHandler in C++
c++;delegates;event handling