CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 1999
    Posts
    6

    How to make a callback function without template?

    I need a class like

    class EventHandler {
    public:
    EventHandler(){}
    void setHandler(...); //accept a pointer to member function like
    // &ClassA:nClick
    int invoke(); //excute the member function
    }



    How to make such a class without template?(because a need a container to keep all the EventHandlers.) Please help me...



  2. #2
    Join Date
    May 1999
    Location
    India
    Posts
    98

    Re: How to make a callback function without template?

    In the 'Invoke' method, you have to freeze the signature of the callback anyway ( unless you have different notifications to be sent for different cases.) Anyway, the best way to handle this is to have an abstract base class as a callback as shown below.


    class CCallback
    {
    virtual void OnInvoke()=0;
    };




    So in the SetHandler method you can pass a ref to this class.


    class EventHandler
    {
    public:
    EventHandler(){}
    void setHandler(CCallback* pCb);
    int invoke();
    };






    In the implementation of 'Invoke', you can blindly invoke CCallback::OnInvoke()

    Since CCallback is abstract, anybody implementing the callback needs to override the 'OnInvoke' method.


  3. #3
    Join Date
    Jun 1999
    Posts
    6

    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 Editublic 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 Editublic 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
  •  





Click Here to Expand Forum to Full Width

Featured