Thread: Function Callback, passing different class members

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Function Callback, passing different class members

    Hi,

    I want to define a callback handler that can take different implementations in the class hierarchy.

    Here is the way in which i tried to do , but get a compilation error
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
            public:
                    void fun(int){}
    };
    
    class B: virtual  public A
    {
            public:
                    void func(int){}
    };
    
    typedef void(A::*TFunPointer)(int);
    
    
    int main()
    {
            A a;
            B b;
            A::TFunPointer t = &A::fun;
            A::TFunPointer t1 = &B::func;
    }
    I got the Following error in compilation with g++
    ------klip-------------------
    funPointer.cxx: In function `int main()':
    funPointer.cxx:24: error: cannot convert `void (B::*)(int)' to `void (A::*)(int)' in initialization
    ------klap-------------------

    Can someone explain why this is not working? Is there any workaround ?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Small change in above code

    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
            public:
                    void fun(int){}
    };
    
    class B: virtual  public A
    {
            public:
                    void func(int){}
    };
    
    typedef void(A::*TFunPointer)(int);
    
    
    int main()
    {
            A a;
            B b;
            TFunPointer t = &A::fun;
            TFunPointer t1 = &B::func;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are two ways to accomplish what you want. Both rely on polymorphism.
    Common code:
    Code:
    
    
    Dynamic polymorphism:
    Code:
    class Callback
    {
    public:
        virtual void operator () (int arg) = 0;
    };
    
    class A: public Callback
    {
            public:
                    virtual void operator () (int arg) {}
    };
    
    class B: public Callback
    {
            public:
                    virtual void operator () (int arg) {}
    };
    
    typedef void (Callback::* Callback_t)(int);
    
    void CallbackFunc(Callback_t fnc, Callback& callback)
    {
        (callback.*fnc)(10);
    }
    
    int main()
    {
        A a;
        B b;
        CallbackFunc(&Callback::operator (), a);
        CallbackFunc(&Callback::operator (), b);
    }
    Static polymorphism (better approach):
    Code:
    class A
    {
            public:
                    void operator () (int arg){}
    };
    
    class B
    {
            public:
                    void operator () (int arg){}
    };
    
    template<typename T>
    void CallbackFunc(T functor)
    {
        functor(10);
    }
    
    int main()
    {
        CallbackFunc(A());
        CallbackFunc(B());
        CallbackFunc([](int arg) { }); // Lambda
    }
    First example is not verified. The second approach is recommended. Again, not tested, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback into a class member function
    By kovacsbv in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2009, 10:06 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM