function/bind - passing function pointer to C API
Hi,
I am attempting to use Boost bind and function to help me pass a member
function as a function pointer to the GLUT C-library.
I have difficulty understanding the usage of bind and function in the
Boost library. When I attempt to pass what I thought was a function pointer,
the compiler indicated that it is actually not a function pointer.
class MyGlut {
public:
MyGlut();
virtual ~MyGlut();
void loop(int argc, char **argv);
void draw(void);
void displayFunc(boost::function
Quoting Nicholas Yue
Hi,
I am attempting to use Boost bind and function to help me pass a member function as a function pointer to the GLUT C-library.
I have difficulty understanding the usage of bind and function in the Boost library. When I attempt to pass what I thought was a function pointer, the compiler indicated that it is actually not a function pointer.
because the result of a bind is a functor not a function pointer. A functor is a class which define operator() so that you can use it like a function. To have a call back in a C librairie, you need a static function pointeur, something which is defined globaly and which can't have parameter bind to it. If you have only one instance of MyGlut, then you can just do:
class MyGlut { public: MyGlut(); virtual ~MyGlut(); void loop(int argc, char **argv); void draw(void);
static void displayFunc(boost::function
private: static void displayCallbackFwd() { display_callback(); } private: static boost::function
display_callback; }; void MyGlut::displayFunc(boost::function
func) { display_callback = func; glutDisplayFunc(MyGlut::displayCallbackFwd); } void MyGlut::loop(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE); glutCreateWindow("Hello World");
//glutDisplayFunc(redraw); // Original way to pass C-function pointer displayFunc( boost::bind(&MyGlut::draw, *this) );
}
Regards
participants (2)
-
Cédric Venet
-
Nicholas Yue