简体   繁体   English

通过回调函数访问实例

[英]Access instance from callback function

How can I access the class instance inside a GLFW3 input callback function, for example this one . 如何在GLFW3输入回调函数(例如this)中访问类实例。

I want my instance do something when a specific event happens. 我希望实例在发生特定事件时执行某些操作。 Each instance might do something different for a specific event. 每个实例可能会对特定事件执行不同的操作。

Specifically, my class has a std::map< int, std::function< void()>>, where a key is mapped to a function. 具体来说,我的班级有一个std :: map <int,std :: function <void()>>,其中一个键映射到一个函数。

EDIT: I tried the following, but this gives me an error that it doesn't match the glfwSetKeyCallback function call. 编辑:我尝试了以下操作,但这给我一个错误,它与glfwSetKeyCallback函数调用不匹配。

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

Taken from here . 取自这里

You need something like this: 您需要这样的东西:

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM