我创建了一个名为 InputControl 的类。我正在使用一个名为 GLFW 的库和一个函数 glfwSetKeyCallback。该函数定义为:
GLFWkeyfun glfwSetKeyCallback ( GLFWwindow * window, GLFWkeyfun cbfun )
类型定义如下:
typedef void(* GLFWkeyfun) (GLFWwindow *, int, int, int, int)
问题是我无法将我的方法转换为函数指针。我觉得我已经尝试以各种可能的方式施展它。我不确定我还需要如何强制转换此方法。是否有特定于将相对方法作为函数指针传递的内容?我的代码如下:
#pragma once
#include <GLFW/glfw3.h>
#include "config.h"
class InputControl {
public:
void bind();
void unbind();
private:
void __glfw__keycallback_bind(GLFWwindow* window, int key, int scancode, int action, int mods);
};
Run Code Online (Sandbox Code Playgroud)
输入控件.cpp
void InputControl::bind() {
glfwSetKeyCallback(__application__window, __glfw__keycallback_bind);
}
void InputControl::unbind() {
}
void InputControl::__glfw__keycallback_bind(GLFWwindow * window, int key, int scancode, int action, int mods) {
//... other code here
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 给了我以下错误
E0167 类型“void (InputControl::*)(GLFWwindow *window, int key, int scancode, int action, int mods)”的参数与类型“GLFWkeyfun”的参数不兼容
非静态成员函数需要一个对象来处理。您需要一个非成员函数或静态成员函数作为回调。如果您绝对需要InputControl在回调中访问类的实例,则可以使用 设置窗口的用户指针glfwSetWindowUserPointer(),然后回调可以使用该指针调用非静态成员函数:
class InputControl {
public:
void bind();
void unbind();
private:
static void keyCallbackStatic(GLFWwindow* window,
int key,
int scancode,
int action,
int mods);
void keyCallback(GLFWwindow* window,
int key,
int scancode,
int action,
int mods);
};
void InputControl::bind() {
glfwSetWindowUserPointer(applicationWindow, this);
glfwSetKeyCallback(applicationWindow, keyCallbackStatic);
}
void InputControl::keyCallbackStatic(GLFWwindow* window,
int key,
int scancode,
int action,
int mods)
{
InputControl* that = static_cast<InputControl*>(glfwGetWindowUserPointer(window));
that->keyCallback(window, key, scancode, action, mods);
}
void InputControl::keyCallback(GLFWwindow* window,
int key,
int scancode,
int action,
int mods)
{
// Do whatever
}
Run Code Online (Sandbox Code Playgroud)
由您来确保您的InputControl对象与您的窗口一样长时间保持活动状态,并且没有其他任何东西将窗口的用户指针设置为除InputControl对象指针之外的任何内容。