|
Boost Users : |
From: Cédric Venet (cedric.venet_at_[hidden])
Date: 2007-08-16 03:19:28
Quoting Nicholas Yue <yue.nicholas_at_[hidden]>:
> 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<void()> func);
> private:
static void displayCallbackFwd() { display_callback(); }
> private:
static boost::function<void()> display_callback;
> };
>
> void MyGlut::displayFunc(boost::function<void()> 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
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net