Unfortunately, the major problem with function pointers is that you can't send any extra data with them, so there isn't a good way to package up the A pointer and send it to setCallback. However, most C callback interfaces also store a user-defined void* for callback data, which I'm hoping you omitted for brevity. With such a void*, you can do something like this:
In this particular case, there was unfortunatelly no additional parameter to pass private data to the C callback registration function.
// C function declared in some library struct E; extern void setCallBack(void (*callback_fct)(E*), void* data);
typedef boost::function
callback; class A{ public: void init(); void callBack(E* e);
private: callback* cb;
// Trampoline function static void do_callback(E* e, void* data) { (*static_cast
(data))(e); } }; void A::init() { cb = new callback(boost::bind(&A::callBack, this, _1)); setCallback(&do_callback, cb); }
Doug
This is a very useful solution that I am likely going to employ if I
have C function pointers that take also private data. Thanks for the
suggestion.
Probably more as a site note (curiosity if you will on a language level):
#include