如何在 GLFW3 输入回调函数中访问类实例,例如 this one .
我希望我的实例在特定事件发生时执行某些操作。每个实例可能会针对特定事件执行不同的操作。
具体来说,我的类有一个 std::map
编辑:我尝试了以下操作,但这给了我一个错误,它与 glfwSetKeyCallback 函数调用不匹配。
glfwSetKeyCallback(window, [this](GLFWwindow * window, int key, int scancode, int action, int mods){
addCommand(m_events.at(key));
});
最佳答案
取自here .
你需要这样的东西:
glfwSetWindowUserPointer(window, this);
glfwSetKeyCallback(window, [](GLFWwindow * window, int key, int scancode, int action, int mods){
Window * win = static_cast<Window *>(glfwGetWindowUserPointer(window));
win->addCommand(win->m_events.at(key));
});
关于c++ - 从回调函数访问实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857964/