how can i pass an object function by reference to another library funcion that needs a funtion with the same args and the same returns?

To call a member function an object is required, but your C function allows you to pass an int, rather than void* which you could use to pass the pointer to the object. Hence, you need to use that int as an index into a global array of pointers to your objects to locate the object:

MyClass* objects[2];

void callback(int idx) {
    objects[idx]->myFunction(idx);
}

int main() {
    MyClass a(1);
    MyClass b(2);

    objects[0] = &a;
    objects[1] = &b;

    functionLibrary(callback, 0);
    functionLibrary(callback, 1);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top