I would like to create a map with an EventHandler of a Base type but insert into that map several derived EventHandlers like such:
std::unordered_map<int, EventHandler<Base>*> maap;
EventHandler<Derived>* e1 = new EventHandler<Derived>();
maap.emplace(std::make_pair(1, e1));
This is possible with pointers of simple objects but here, EventHandler<> is a templated object so the compiler is fussing about converting. It would be nice if I could do something like
template <class T>
std::unordered_map<int, EventHandler<T>> maap;
But that doesn't work either... Any ideas?
I sort of came up with a solution. I made an empty abstract class IEventHandler and inherited EventHandler from that class. Then I make a map of IEventHandler* and it seems to work fine for now. It can have EventHandler of any type added. I need to now find a way to make sure IEventHandler is only inherited by EventHandler and that it is only of the proper T type.
2 little updates: I used a static_cast to be able to call the EventHandlers method, and because it is casting to a pointer there is no extra copy constructor called ^_^ I restricted who can derived from IEventHandler by giving it a private destructor and friending EventHandler