简体   繁体   中英

Access instance from callback function

How can I access the class instance inside a GLFW3 input callback function, for example this one .

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.

EDIT: I tried the following, but this gives me an error that it doesn't match the glfwSetKeyCallback function call.

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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