How to use base class & child class as parameter in one function ?
Vinod K Chandran
kcvinu82 at gmail.com
Fri May 22 12:12:34 UTC 2020
Hi all,
I have a windows gui setup like this;
class EventArgs {} \\ Base class for all messages
class MouseEventArgs : EventArgs { // child class for handling
mouse messages
...
int x;
int y;
this(WPARAM wpm, LPARAM lpm){
this.x = xFromLparam(lpm);
this.y = yFromLparam(lpm);
}
}
alias EvtFuncPtr = void function(EventArgs); // function pointer
struct MsgHandler {
uint message ;
HWND handle ;
bool isActive ;
EvtFuncPtr fnPtr;
}
// Then in my window class...
void addHandler(uint we, EvtFuncPtr fnp){ // This is error point.
auto mh = MsgHandler();
mh.handle = this.mHandle;
mh.message = we;
mh.fnPtr = fnp;
mh.isActive = true;
this.msgHandlerList ~= mh; // This is a list<MsgHandler> in
window class.
}
// And in the WndProc...
auto thisWin = findWindowClass(hWnd); // get the window class
with hWnd.
auto mh = thisWin.findHandler(hWnd, message); // get the event
handler for this message & hWnd
if(mh.isActive) { // if there is an event handler fixed,
switch (message){
case 512 : .. case 526 : // if it's a Mouse messages
auto ea = new MouseEventArgs(wParam, lParam);
mh.fnPtr(ea); // execute that event handler function
break;
default : break;
}
}
// And this is the window creation site...
auto app = new Application() ;
auto frm = new Window(app) ;
frm.createWindow() ;
frm.addHandler(frm.load, &onLoad);
frm.addHandler(frm.Click, &onClick);
void onLoad(EventArgs e){
log("form is loaded...");
}
void onClick(MouseEventArgs e){ // Compiler wants to change the
MouseEventArgs with EventArgs.
log("form clicked on x = ", e.x, ", and y = ", e.y);
}
I wrote EventArgs as the parameter type in function pointer but
i want to use it's child classes also. But i can't.
More information about the Digitalmars-d-learn
mailing list