You cannot use non-static
member functions for C callbacks.
However, usually C callbacks have a user data pointer that's routed to the callback. This can be explored to do what you want with the help of a static
member functions:
// Beware, brain-compiled code ahead!
typedef void (*callback)(int blah, void* user_data);
void some_func(callback cb, void* user_data);
class my_class {
public:
// ...
void call_some_func()
{
some_func(&callback_,this);
}
private:
void callback(int blah)
{
std::cout << blah << '
';
}
static void callback_(int blah, void* user_data)
{
my_class* that = static_cast<my_class*>(user_data);
that->callback(blah);
}
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…