|
-
July 2nd, 1999, 07:41 AM
#3
Re: How to make a callback function without template?
Hi,Thank you for your help.
That's a good idea,but it seems a little complex to use.I love the eventHandler in VJ6.
class Control{
public:
Control();
virtual int onClick();
}
class Edit ublic Control{
public:
Edit();
virtual init(){
//elegant,isn't it? How to implement?
addEvenHandler(new EventHandler(&Edit: nClick));
addEvenHandler(new EventHandler(&Edit: nKeyPress));
}
virtual int onclick();
virtual int onKeyPress();
}
I tried this way:
#include <iostream>
using namespace std;
class Control{
public:
Control(){}
virtual int onClick(){
cout<<"Control: Click"<<endl;
return 1;
}
};
class Edit ublic Control{
public:
Edit(){}
virtual int onKeyPress(){
cout<<"Control: KeyPress"<<endl;
return 1;
}
};
class EventHandler{
private:
Control* m_pObject;
int (Control::*m_pMemFun)();
public:
template <class Obj,class PtrMemFun>
EventHandler(Obj* o,PtrMemFun pf){
m_pObject=o;
m_pMemFun=(PtrMemFun)pf;
}
int invoke(){
return (m_pObject->*m_pMemFun)();
}
};
int main(){
Edit* edit1=new Edit();
//ok
EventHandler* e=new EventHandler(edit1,&Edit: nClick);
e->invoke();
//error:cannot convert from 'int(__thiscallEdit::*) void)' to 'int (__thiscall Control::*)(void)'
//Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
//see reference to function template instantiation '__thiscall EventHandler::EventHandler(class Edit *,int (__thiscall Edit::*)(void))' being compiled
EventHandler* e2=new EventHandler(edit1,&Edit: nKeyPress);
e2->invoke();
return 0;
}
The problem is: How to convert one kind of member function pointer to other kind of member function pointer?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|