Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple inheritance on different template types
    text
    copied!<p>I'm working on event handling in C++ and to handle notification of events, I have a class EventGenerator which any class generating events can inherit from. EventGenerator has a method which other classes can use to add in callbacks and a method to call the callbacks once an event happens</p> <p>To handle notification of different types of events, I've parametrized EventGenerator on template type T and the notifier class can then inherit from EventGenerator multiple times parametrized on different types.</p> <p>For the sake of completeness, here's the code for EventGenerator</p> <pre><code>#ifndef _EventGenerator #define _EventGenerator #include &lt;list&gt; #include "EventListener.h" template &lt;class Event&gt; class EventGenerator { private: std::list&lt;EventListener&lt;Event&gt;*&gt; listeners; protected: EventGenerator() {} void changeEvent(Event event) { std::list&lt;EventListener&lt;Event&gt;*&gt;::const_iterator it = listeners-&gt;begin(); for (; it != listeners-&gt;end(); it++) { (*it)-&gt;changeEvent(event); } } public: void addListener(EventListener&lt;Event&gt;* listener) { listeners-&gt;push_back(listener); } }; #endif </code></pre> <p><br> <br> and here's the code for EventListener which any class which wants to add callbacks inherits from -</p> <pre><code>#ifndef _EventListener #define _EventListener template &lt;class Event&gt; class EventListener { private: EventListener(const EventListener&lt;Event&gt;&amp; event); protected: EventListener() {} public: virtual void changeEvent(Event event) = 0; }; #endif </code></pre> <p><br> <br> I've a feeling this is not a very good design and was wondering if there was a better design out there for such a problem.</p> <p>Edit: What bothers is the fact that I'm using multiple inheritance. I've been frequently warned against using it so I guess I wanted opinions on whether such a design could lead to bad things happening in the future</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload