Hey all... I haven't in the past done much in C++, most of my PPC development has been mostly straight C. I'm doing something now that is OO, and I'm facing an issue, and because my OOP experience is almost entirely Java, I'm not quite sure how to deal with it in C++...
Here's what I have... I have a base class:
This is to be an event handler class that will be "registered" with another class, which has the following:
The idea of course being that a client would create a subclass of OGEventHandler, implementing the appropriate methods, register it with an OGWidget object, which will dispatch as appropriate to the registered event handler class. Here's an example of usage:
Ok, so now we come to the problem... Everything works exactly as expected until the event is dispatched... It in facts calls the click() method in the OGEventHandler class, NOT the click() method in the Button1EventHandler class. In other words, I'm getting the method of the parent class rather than the subclass.
It makes sense to me based on my Java experience... because the eventHandler pointer in OGWidget is of type OGEventHandler, it only knows about the click() implementation in that exact type, not the subclass type.
My question though is how to deal with this? In Java I would of course cast eventHandler to Button1EventHandler, but since the OGWidget class would never know about the subclass, I don't see how I can do that.
Am I missing something obvious, or is this not possible? Or is a refactoring of some sort required to make it work?
Thanks in advance!
Here's what I have... I have a base class:
- Code:
class OGEventHandler {
public:
void click() { }
};
This is to be an event handler class that will be "registered" with another class, which has the following:
- Code:
class OGWidget {
public:
OGEventHandler* eventHandler;
void registerEventHandler(OGEventHandler* in_eventHandler) {
eventHandler = in_eventHandler;
}
void dispatchEvent(int eventType) {
if (eventHandler != NULL) {
switch (eventType) {
case OG_EVENT_CLICK: {
eventHandler->click();
break; }
}
}
}
};
The idea of course being that a client would create a subclass of OGEventHandler, implementing the appropriate methods, register it with an OGWidget object, which will dispatch as appropriate to the registered event handler class. Here's an example of usage:
- Code:
// Class definition for the event handler specific to the button I will create
class Button1EventHandler : public OGEventHandler {
public:
void click() {
MBT(TEXT("click!")); // Just displays a popup
}
};
...then later on...
// pButton is a valid pointer to an object of type OGButton, a subclass of OGWidget
pButton->setText(TEXT("My First Button"));
pButton->setFont(fntVGA);
pButton->show();
Button1EventHandler* eh = new Button1EventHandler();
pButton->registerEventHandler(eh);
Ok, so now we come to the problem... Everything works exactly as expected until the event is dispatched... It in facts calls the click() method in the OGEventHandler class, NOT the click() method in the Button1EventHandler class. In other words, I'm getting the method of the parent class rather than the subclass.
It makes sense to me based on my Java experience... because the eventHandler pointer in OGWidget is of type OGEventHandler, it only knows about the click() implementation in that exact type, not the subclass type.
My question though is how to deal with this? In Java I would of course cast eventHandler to Button1EventHandler, but since the OGWidget class would never know about the subclass, I don't see how I can do that.
Am I missing something obvious, or is this not possible? Or is a refactoring of some sort required to make it work?
Thanks in advance!