使用成员函数作为回调 [英] Use a member function as callback

查看:73
本文介绍了使用成员函数作为回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将成员函数用作回调(使用此函数):

I would like to use a member function as a callback (using this function):

glfwSetCursorPosCallback(window, (GLFWcursorposfun)(MyClass::mouseButtonChanged));

我知道这是不可能的,因为我需要 MyClass的实例调用方法 mouseButtonChanged

I know it is not possible since I need an instance of MyClass to call the method mouseButtonChanged.

但是我该怎么办?

推荐答案

您可以使用 glfwSetWindowUserPointer 附加一个管理窗口的C ++类。之后,您可以编写静态函数以转发到 WindowManager的成员函数

You might use glfwSetWindowUserPointer to attach a C++ class managing the window. After that you can write a static function forwarding to to a member function of the 'WindowManager'

来自 http://www.glfw.org/faq.html#how-do-i-use-c-作为回调的方法


2.16-如何使用C ++方法作为回调?

2.16 - How do I use C++ methods as callbacks?

您不能将常规方法用作回调,因为GLFW是C语言库
,并且不了解对象和该指针。如果您希望
接收到C ++对象的回调,请使用静态方法或常规
函数作为回调,将指向您希望
调用的对象的指针存储为窗口的用户指针,用它来调用对象
上的方法。

You cannot use regular methods as callbacks, as GLFW is a C library and doesn’t know about objects and this pointers. If you wish to receive callbacks to a C++ object, use static methods or regular functions as callbacks, store the pointer to the object you wish to call as the user pointer for the window and use it to call methods on your object.

这篇关于使用成员函数作为回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆