Why callback functions needs to be static when declared in class

Posted by Dave17 on Stack Overflow See other posts from Stack Overflow or by Dave17
Published on 2010-03-08T10:43:11Z Indexed on 2010/03/08 10:51 UTC
Read the original article Hit count: 511

Filed under:
|

I was trying to declare a callback function in class and then somewhere i read the function needs to be static but It didn't explain why?

#include <iostream>
using std::cout;
using std::endl;

class Test
{
public:
    Test() {}

    void my_func(void (*f)())
    {
        cout << "In My Function" << endl;
        f(); //Invoke callback function
    }

    static void callback_func()
    {cout << "In Callback function" << endl;}
};

int main()
{
    Test Obj;
    Obj.my_func(t.callback_func);
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about callback