4

I have the following problem. I have to use a function that takes a callback. Implementing the callback is the tricky part, because I need more information beyond that I can extract from the input parameters. I will try to give an example:

typedef int (*fptr) (char* in, char* out); // the callback i have to implement
int takeFptr(fptr f, char* someOtherParameters); // the method i have to use

The problem is that I need additional info except the "in" parameter to construct the "out" parameter. I tried this approach:

class Wrapper {
    public:
        int callback(char* in, char* out){
           // use the "additionalInfo" to construct "out"
        }
        char* additionalInfo;
}

...
Wrapper* obj = new Wrapper();
obj->additionalInfo = "whatIneed";
takeFptr(obj->callback, someMoreParams);

I get the following error from the compiler:

error: cannot convert 'Wrapper::callback' from type 'int (Wrapper::)(char*, char*)' to type 'fptr {aka int(*)(char*, char*)}'

egelev
  • 1,025
  • 2
  • 12
  • 24
  • 3
    Ugh. If there's no hope of changing the interface you need to use to take a `std::function` or something, then `bind` or similar can't help you. You'll just have to create a wrapper global function that knows where to find some instance of `Wrapper` and call `callback` on it. This sucks because it effectively forces you to have some kind of global data. – BoBTFish Aug 01 '14 at 10:08
  • 1
    see: http://stackoverflow.com/questions/1000663/using-a-c-class-member-function-as-a-c-callback-function – Icarus3 Aug 01 '14 at 10:09
  • Even if you could convert from a pointer to member to a normal function pointer (you can't in portable code[*](https://gcc.gnu.org/onlinedocs/gcc/Bound-member-functions.html)), how do you expect the `this` pointer to be available to the callback? – Jonathan Wakely Aug 01 '14 at 10:11

1 Answers1

4

You need to pass what you need to pass, in this case a pointer to a function.

::std::function<int (char*, char*)> forwardcall;

int mycallback(char* in, char* out) // extern "C"
{
  return forwardcall(in, out);
}

forwardcall can contain any functor, for example:

forwardcall = [obj](char* in, char* out){ return obj->callback(in, out); };
user1095108
  • 12,675
  • 6
  • 43
  • 96