c++ - 函数可以接受静态函数指针作为参数吗?

标签 c++ function-pointers glut static-functions

这是一个很好的例子:我正在尝试重载 OpenGL 的 glutMouseFunc,以便它可以接受我选择的命名空间和类函数。特别是 Init::DisplayInit::mouse,它是静态的。问题是,这可能吗?如果是这样,这是如何实现的?

我的实现

void glutMouseFunc(void (Init::DisplayInit::*mouse)(int, int, int, int)) {
    (*mouse);
}

实现错误

..\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

请注意,我将函数的声明放在同一个文件的头文件中。我还确保声明 函数的定义位于 namespace 声明之外(它包装了两个文件的大部分,每个)。如图所示,第一个错误是将函数读取为变量或字段 (???)。

最佳答案

这不是定义 glutMouseFunc 的合理方式。它不应该立即调用回调,它应该为以后保存一个指针(当鼠标事件发生时)。

调用 GLUT 提供的版本,并传递函数的地址:

#include <GL/glut.h>
glutMouseFunc(&Init::DisplayInit::mouse);

静态成员函数与普通函数指针兼容。

关于c++ - 函数可以接受静态函数指针作为参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675534/

相关文章:

c++ - 尝试在 C++ 中执行某些 OpenGl 时出现 C2664 错误

c++ - 在原生 Tizen TV 上使用 Elementary 访问多次阅读文本

C++11,常量数据成员,std::inserter,复制

c++ - 没有名字的函数

c - 如何用C实现曲线的自适应 segmentation 算法

python - 来自 PyOpengl 缓冲区的 PIL Image.fromstring 大小错误

c++ - 返回字符串的静态方法

c++ - 带有 std::array 的矩阵类

c++ - 指向独立函数和 friend 函数的指针之间的区别

c++ - 如何将成员函数传递给类构造函数?