且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从同一个类的静态函数访问一个类的私有成员?

更新时间:2023-02-15 17:25:38

静态成员函数是 class 的一部分,并且没有与之关联的对象实例(换句话说,没有静态成员函数中的指针).为了能够访问非静态成员变量,您需要一个类的实际实例.

Static member functions are part of the class, and have no object instance associated with them (in other words, there's no this pointer in static member functions). To be able to access non-static member variables you need an actual instance of the class.

使用旧的C库设置回调时,常见的解决方案是使用某种用户数据指针,并将其分配给该类的实例.对您来说幸运的是,GLFW库您可以使用这样的指针

A common solution when setting callbacks using old C libraries, is to use some kind of user-data pointer, and assign it to an instance of the class. Fortunately for you, the GLFW library have such a pointer that you can use.