使用非静态成员功能设置GLFW的回调功能
我正在使用GLFW来设置OpenGL可视化。我想将回调函数设置为非静态功能以访问类变量。但是,当我在下面做时,我会收到“必须调用对非静态成员函数的引用”。我知道在这种情况下,回调函数必须是静态的,但是有没有办法实现目标?
class Viewer
{
public:
Viewer()
{
glfwSetErrorCallback(errorCallback);
}
private:
void errorCallback(int error, const char* description)
{
// print more class variables
std::cerr << "Error: " << description << std::endl;
}
};
I'm using glfw for setting up the OpenGL visualization. I would like to set the callback function as non-static function to access the class variables. However, when I did below I got the error "reference to non-static member function must be called". I know that the callback function must be static in this case, but is there a way to achieve the objective?
class Viewer
{
public:
Viewer()
{
glfwSetErrorCallback(errorCallback);
}
private:
void errorCallback(int error, const char* description)
{
// print more class variables
std::cerr << "Error: " << description << std::endl;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GLFW是C接口,不支持C ++对象。对于窗口,监视和操纵杆回调函数,您可以为每个对象存储一个用户指针,将其检索到回调函数中,并使用它来调用对象的成员函数(方法)。
错误回调是不可能的。相反,您可以使用全局变量存储指向本地错误处理程序的指针。如果此变量不是
nullptr
,则全局错误回调将调用此错误处理程序。您在调用GLFW函数之前设置此指针,然后将其恢复或将其无效。请注意,这种简单的方法仅适用于单线程应用程序。如果您从多个线程调用GLFW函数,则需要确保线程安全。 GLFW 4可能会改善这一点。
我的C ++非常生锈,所以不要指望它可以解决问题,但我相信它可以证明您可以做什么:
另一种可能性可能是在全球错误处理程序中扔C ++错误,并在该位置捕获它这调用了有问题的GLFW功能。
GLFW is a C interface, and does not support C++ objects. For the window, monitor and joystick callback functions, you can store a user pointer for each of these objects, retrieve it in the callback functions, and use that to call a member function (method) of your objects.
This is not possible with the error callback. Instead, you can use a global variable to store a pointer to a local error handler. The global error callback calls this error handler if this variable is not a
nullptr
. You set this pointer before calling a GLFW function, and restore it or nullify it afterwards.Note that this simple approach only works for single-threaded applications. If you are calling GLFW functions from multiple threads, you need to ensure the thread safety. GLFW 4 will likely improve upon this.
My C++ is very rusty, so don't expect this to work out of the box, but I believe it demonstrates what you can do:
Another possibility may be to throw a C++ error in the global error handler, and catch it at the location that calls the offending GLFW function.