I need to call a non static member function from a static member function of the same class. The static function is a callback. It can receive only void as data, though whic
If your instance is a singleton (usually implemented using a private or protected constructor and a static pointer to itself) you can do e.g.:
class MyClass {
private:
MyClass():myInstance(0) {}
MyClass *myInstance;
void callback();
public:
~MyClass() {}
static MyClass *getInstance();
static void myCallback();
};
MyClass *MyClass::getInstance() {
if(!myInstance) {
myInstance = new MyClass;
}
return myInsance;
}
void MyClass::callback() {
// non-static callback
}
void MyClass::myCallback() {
getInstance()->callback();
}
If you don't use a singleton but you can pass the instance cast to a void * then you can do this instead:
void MyClass::myCallback(void *data) {
MyClass *instance = static_cast(data);
instance->callback();
}