glutTimerFunc issues
I am using a timer function to animate, but I am having issues when I place it in the Renderer class.
void Renderer::animate(int value)
{
glutTimerFunc(TIMERMSECS, animate, 0);
}
Error: \renderer.cpp(242) : error C3867: 'Renderer::animate': function call missing argument list; us开发者_高级运维e '&Renderer::animate' to create a pointer to member
The issue is that Renderer::animate
is a class member function and so has a hidden this
parameter. glutTimerFunc
doesn't know what value of this
to pass, so if you somehow tried to sneak past the compiler with a nasty cast, it would crash at runtime.
The solution is to change Renderer::animate
into a static method or an ordinary global function. You then need to store the pointer to your Renderer
instance in a global variable, e.g.:
class Renderer
{
static void staticAnimate(int value);
void animate(int value);
...
};
Renderer *gRenderer = ...;
void Renderer::staticAnimate(int value)
{
gRenderer->animate(value);
}
...
glutTimerFunc(TIMERMSECS, &Renderer::staticAnimate, 0);
If sizeof(void*) <= sizeof(int)
(true on 32-bit systems but not true on 64-bit systems), then you could also pass the instance via the value
parameter; however, since this is not portable and you won't ever need to call glutTimerFunc
on multiple different instances simultaneously, you shouldn't have to worry about using a global variable for this.
glutTimerFunc()
expects a pointer to a function of type void (*func)(int value)
, not a member function of type void (Renderer::*func)(int value)
.
Make Render::animate
static or use a global function.
精彩评论