473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with virtual in derived class

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?


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
1 1516
On Tue, 27 Apr 2004 17:07:40 -0400, Bren <ia***********@sympatico.ca>
wrote:

Crap, that enum in A:
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! :)

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
8
by: lmfmaw | last post by:
Hi all, I've hit the wall with this "fairly" simple problem. As you can see in the code below, my destructors don't do their job as they are supposed to (I left them empty for this example). I'm...
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.