c++ - 从回调函数访问实例

标签 c++ callback glfw

如何在 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/

相关文章:

C++ 转换 char int ARM

c++ - Linked List no match for operator* 编译器错误

c++ - 如果将非常简单的函数定义移动到 .cpp,编译时间会减少多少?

java - 如何在Spring上下文中注入(inject)回调类?

c++ - 模板实例化错误,MSVC 与 GCC

javascript - 为什么在语法更简洁的情况下使用 javascript 回调?

json - 从 JSON Api 快速请求数据

C++ OpenGL 程序 - 添加图标

c++ - 使用链接 linux 编译 GLFW 应用程序问题

c++ - GLFW/GLEW C++ (atioglxx.dll)