Hey,
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
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.
For the sake of completeness, here's the code for EventGenerator
#ifndef _EventGenerator
#define _EventGenerator
#include <list>
#include "EventListener.h"
template <class Event>
class EventGenerator {
private:
std::list<EventListener<Event>*> listeners;
protected:
EventGenerator() {}
void changeEvent(Event event) {
std::list<EventListener<Event>*>::const_iterator it = listeners->begin();
for (; it != listeners->end(); it++) {
(*it)->changeEvent(event);
}
}
public:
void addListener(EventListener<Event>* listener) {
listeners->push_back(listener);
}
};
#endif
and here's the code for EventListener which any class which wants to add callbacks inherits from -
#ifndef _EventListener
#define _EventListener
template <class Event>
class EventListener {
private:
EventListener(const EventListener<Event>& event);
protected:
EventListener() {}
public:
virtual void changeEvent(Event event) = 0;
};
#endif
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.
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
Thanks Sid