zeb*_*und 2 c++ glut function-pointers static-functions
这是一个很好的例子:我正在尝试重载OpenGL,glutMouseFunc因此它可以接受我选择的命名空间和类功能.特别是Init::DisplayInit::mouse,这是静态的.问题是,这可能吗?如果是这样,这是如何实现的?
我的实施
void glutMouseFunc(void (Init::DisplayInit::*mouse)(int, int, int, int)) {
(*mouse);
}
Run Code Online (Sandbox Code Playgroud)
实施中的错误
..\OpenGL_03\/displayinit.h:27: error: variable or field 'glutMouseFunc' declared void
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: void value not ignored as it ought to be
Run Code Online (Sandbox Code Playgroud)
注意,我把函数的声明放在同一个文件的头文件中.我还确保函数的声明和定义都位于名称空间声明之外(它包含了大部分文件,每个文件).如图所示,第一个错误之一将函数读作变量或字段(???).
这不是一种合理的定义方式glutMouseFunc.它不应该立即调用回调,它应该为以后保存指针(当鼠标活动发生时).
调用GLUT提供的版本,并传递函数的地址:
#include <GL/glut.h>
glutMouseFunc(&Init::DisplayInit::mouse);
Run Code Online (Sandbox Code Playgroud)
静态成员函数与普通函数指针兼容.
| 归档时间: |
|
| 查看次数: |
297 次 |
| 最近记录: |