Problem with virtual in derived class

B

Bren

I'm hoping somebody can help me with the following problem. I'm
hoping it's not insurmountable.

Here's a basic structure:

class EventTarget
{
public:
EventTarget();
virtual ~EventTarget();

virtual eResult OnEvent(Event* event);
};

class Node : public EventTarget
{
public:
Node();
virtual ~Node();
};

class A : public Node
{
public:
A();
~A();

enum eResult { ER_PROCESSED, ER_ERROR };

eResult OnEvent(Event* event);
};

class EventManager
{
public:
EventManager();
~EventManager();

void RegisterEventTarget(UINT event, EventTarget* target);
void UnregisterEventTarget(UINT event, EventTarget* target);

void PostEvent(UINT event, EventTarget* target = 0);
eResult SendEvent(UINT event, EventTarget* target);
};

class Event
{
public:
Event(UINT event, EventTarget* target = 0);
~Event();

UINT GetEvent() { return m_Event; }
EventTarget* GetTarget() { return m_EventTarget; }
private:
UINT m_Event;
EventTarget* m_EventTarget;
};

OK, so I would like Node and EventTarget to be seperate so you can
have an EventTarget that is not a Node.

EventManager handles the dispatching of events, and the registration
of EventTargets. The idea is that EventTargets can register for
certain events. PostEvent queues events for dispatch (similar to
Win32's PostMessage), and SendEvent is a blocking call that targets a
specific EventTarget and returns the result (SendMessage). Events can
also be "broadcast" to all EventTargets that have registered for a
given event by calling PostEvent with no EventTarget specified.

RegisterEventTarget is typically called from within the derived class
(i.e. A) using "this", and is stored in EventManager using a <map> of
event UINTS to a <list> of EventTargets. Queued events are then
processed, sent to all EventTargets that have registered for that
event, or only to specified EventTarget if one was passed in the
PostEvent call.

Besides that, inside EventManager, the actual call to OnEvent is the
same whether the Event is a broadcast PostEvent, a targeted PostEvent,
or a targeted SendEvent.

Finally, I want to be able to call SendEvent(event, node), where node
is an instance of a Node class, in addition to the explicitly defined
SendEvent(event, target) where target is a EventTarget derived class.

Now, everything builds and runs fine. No linking or run-time errors.
The problem is that SendEvent doesn't reach the derived A::OnEvent, it
just goes into EventTarget::OnEvent. HOWEVER, events that are queued
by a call to PostEvent do succesfully reach A::OnEvent. I'm really
perplexed because, as I mentioned, within EventManager, everything is
handled as EventTargets, whether it is a class derived from Node or
directly from EventTarget.

The ONLY difference I can see is that SendEvent is usually passed a
Node*, whereas the targets for PostEvents are registered with "this"
from within a EventTarget derived class. For example, A can register
for an event using "this" and will get PostEvents sent to its OnEvent,
but SendEvent(event, a) will go straight to EventTarget::OnEvent.

I've tried NOT making Node inherit from EventTarget (though I would
really like it to be just for ease of use) and making A inherit from
both Node and EventTarget, but it behaved the same at run time. This
precludes SendEvent(event, node) which I would really like to do,
anyway.

This system could be implemented using virtual methods instead of the
events, but I really want it to be extensible in terms of
EventTarget/Node derived classes, and additional events.

Does anybody see what the problem is here? Any suggestions?
 
B

Bren

class A : public Node
{
public:
A();
~A();

enum eResult { ER_PROCESSED, ER_ERROR };

eResult OnEvent(Event* event);
};

.... should have been down there EventManager:
class EventManager
{
public:
EventManager();
~EventManager();

void RegisterEventTarget(UINT event, EventTarget* target);
void UnregisterEventTarget(UINT event, EventTarget* target);

enum eResult { ER_PROCESSED, ER_ERROR };

void PostEvent(UINT event, EventTarget* target = 0);
eResult SendEvent(UINT event, EventTarget* target);
};

Apologies. Very tired ATM. Maybe THAT's the problem! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,348
Latest member
RoscoeNevi

Latest Threads

Top