开发者

Moving callback function to a class causes an error!

After I moved some OpenGL code from main function to a new class I had the following error on the following row:

glutDisplayFunc(OnDisplay);

error C3867: 'Room::OnDisplay': function call missing argument list; use '&Room::OnDisplay' to create a po开发者_StackOverflow中文版inter to member

What was my fault ?


glutDisplayFunc expects a void (*func)(void), but you're passing in a void (Room::*func)(void).

Since class methods receive an implicit this parameter, their pointer types are fundamentally different than regular function pointers. There's no conversion possible between them.

All you can do is make OnDisplay a static member of Room. From there you can forward the call to a member function of a concrete Room instance (since there is by design only one glut display callback and you migrated from procedural code, I presume you have only a single Room object somewhere).


glutDisplayFunc just takes pointer to the function. When moved OnDisplay to the class, you will also pass the hidden argument this to glutDisplayFunc when actually get called.

One possible solution is to make OnDisplay as a static method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜