从回调函数访问实例

时间:2014-04-04 08:54:08

标签: c++ callback glfw

如何访问GLFW3输入回调函数中的类实例,例如this one

我希望我的实例在特定事件发生时执行某些操作。每个实例可能会针对特定事件执行不同的操作。

具体来说,我的类有一个std :: map< int,std :: function< void()>>,其中键被映射到函数。

编辑:我尝试了以下操作,但这给了我一个错误,它与glfwSetKeyCallback函数调用不匹配。

glfwSetKeyCallback(window, [this](GLFWwindow * window, int key, int scancode, int action, int mods){
    addCommand(m_events.at(key));
});

1 个答案:

答案 0 :(得分:1)

取自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));

});
相关问题