hi, im trying to write a program that parses messages using classes derived from a stub hook class.
Code:
uint32_t HookManager::addReadHook (uint8_t id, ReadHook* hook) {
if (!hook) {
printf ("warning: attempt to assign NULL read hook\n");
return -1;
}
uint32_t hid = ids->newId ();
rLookup.insert (std::pair<uint32_t, uint32_t> (hid, id));
rHooks[id].insert (std::pair<uint32_t, ReadHook> (hid, *hook));
printf ("insert: %p\n", hook);
return hid;
}
void HookManager::hookReadMessage (Message* m, Client* client)
{
uint8_t id = m->getId ();
for (RHookList::iterator it = rHooks[id].begin ();
it != rHooks[id].end (); ++ it)
{
(*it).second.func (m, client);
}
}
class ReadHook
{
public:
virtual ~ReadHook () {};
virtual void func (Message* m, Client* client) {}
};
rHooks[i] is a map of hooks, and i want to iterate through the map calling each derived classes func. I used to store the derived hooks in a list<ReadHook*> and called the func using (*it)->func () and this worked. Whilst cleaning up the code i thought it would be nicer to store them as ReadHook, but now instead of the derived classes func being called the base classes func gets called. Ive tested this by defining the base func as {printf ("blah\n");}. Is there a way to do what i want do to, or is this a limitation of c++. thanks.