glfwGetWindowUserPointer 有什么作用?

What does glfwGetWindowUserPointer do?

在查看 GLFW 参考时,我遇到了 glfwGetWindowUserPointer 函数(和 glfwSetWindowUserPointer 函数)。在参考中,它说了以下关于用户指针的内容:

Each window has a user pointer that can be set with glfwSetWindowUserPointer and fetched with glfwGetWindowUserPointer. This can be used for any purpose you need and will not be modified by GLFW throughout the life-time of the window.

现在我想知道这个可以用于什么目的?

我不会把这个答案归功于我,因为这不是我的答案,而是 GLFW forum 上其他人的答案。

A UserData field is a fairly common paradigm in C APIs that lets the user access contextual data from within callbacks without needing to make everything global. In essence, it lets you associate an arbitrary piece of data relevant to your program with a glfw window.

If you are trying to use glfw in a program that follows object-oriented design, for example, you can use this pointer to store the address of the instance that is handling a particular window, and forward the callbacks (which have to be static functions, because of the way the API works) to the appropriate member functions.