从回调函数访问实例
Access instance from callback function
如何访问 GLFW3 输入回调函数中的类实例,例如这个。
我希望我的实例在特定事件发生时执行某些操作。每个实例可能会对特定事件执行不同的操作。
具体来说,我的类有一个 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));
});
取自这里。
你需要这样的东西:
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));
});
相关文章:
- 初始化指向类实例的智能指针并访问其方法
- 如何从实现接口的模板化类实例访问结构
- 访问同级类实例的成员
- 从模板参数包实例化的访问类实现
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 限制多模板参数朋友函数可访问的类实例的范围
- 从 QT Widget 对象实例访问 QT Widget 子成员
- 为什么 clang 不允许通过实例访问嵌套枚举类?
- 从C++中的父类实例访问子类方法
- 我可以从 C++ 中的派生实例访问指向基本实例的指针吗?
- c++单例类实例访问整个应用程序
- 为什么我无法从基类的实例访问受保护的成员?
- 为什么可以从派生实例访问基类中的受保护变量
- 尝试从从 DLL 加载的驱动类实例访问基类向量成员时,程序崩溃
- 通过外部类的实例访问嵌套的内部类
- 通过对象实例访问私有静态成员
- 如何在c++中从一个实例访问另一个实例的信息
- 任何实例访问(类的)所有实例
- 从子类实例访问和更改父值
- 在c++中,通过指针或实例访问类/结构的优点是什么?