I'm working on an application using Qt version 4.8 in C++.
I need to use a C function which requires a pointer to a free function:
void handler(int dummy)
I need to change the displayed values in my GUI depending on what happens inside the function.
So I've been reading on how to convert a member function to a free function. I've found out that I can do it only if my member function is static.
But I can't make it static - I won't be able to change any widgets if I make it static.
So, how can I deal with the problem? How can I pass a non-static member function as a pointer to a free function. If it's not possible, how to make a workaround?
I'll be grateful for any suggestions different that use another library.
Yes, C++ does not allow member functions to be passed as a function pointer to C libraries.
In this cases a common approach is using a helper function that acts like a bridge between the C library and our class instance. This helper function is a free function that takes a pointer to the current instance of the class and in its body invoke the corresponding member method of the class instance.
And where you invoke a function from that C library, you should pass
handler_helper
as the function pointer and(int) this
as its argument.