# What is a possible use of glfwGetWindowUserPointer?

**URL:** <https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294>\
**Category:** support\
**Created:** [March 14, 2019, 4:52pm UTC](https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294 "2019-03-14T16:52:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deerangle2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/deerangle2/32/275_2.png) [@deerangle2](https://discourse.glfw.org/u/deerangle2)\
**Post date:** [March 14, 2019, 4:52pm UTC](https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294/1 "2019-03-14T16:52:50Z")

</div>

I asked this same [question](https://stackoverflow.com/questions/55145966) on StackOverflow but didn’t get any answeres there, so I figured this place would be more suitable.

When looking through the GLFW reference I came accross the `glfwGetWindowUserPointer` function (and the `glfwSetWindowUserPointer` function). In the reference it says the following about the user-pointer:

> 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.

Now I wonder for what purpose one could use this?

---

<div class="post-metadata">

**Author:** ![tombsar](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@tombsar](https://discourse.glfw.org/u/tombsar)\
**Post date:** [March 14, 2019, 5:42pm UTC](https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294/2 "2019-03-14T17:42:43Z")

</div>

Welcome to the 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. e.g.

```auto
/* Constructor for the MyWindowHandler class */
MyWindowHandler (GLFWwindow * window) {
    ....
    glfwSetWindowUserPointer(window, reinterpret_cast<void *>(this));
}

...

/* Key event callback wrapper function */
static void key_callback (GLFWwindow * window, int key, int scancode, int action, int mods) {
    MyWindowHandler * handler = reinterpret_cast<MyWindowHandler *>(glfwGetWindowUserPointer(window));
    if (handler)
        handler->keyEvent(key, scancode, action, mods);
}

```

---

<div class="post-metadata">

**Author:** ![supersim135](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@supersim135](https://discourse.glfw.org/u/supersim135)\
**Post date:** [February 2, 2020, 4:48am UTC](https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294/3 "2020-02-02T04:48:43Z")

</div>

Does using a static\_cast or reinterpret\_cast keyword allow you to access the object as if you were within it? If not, what does it do?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 3, 2020, 12:39pm UTC](https://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294/4 "2020-02-03T12:39:48Z")

</div>

Hi @supersim135 welcome to the GLFW forum!

I’m not sure what your sentence _allow you to access the object as if you were within it_ means. If you mean can you access private members of a class then no, neither of these casts do that.

You might find the examples in the documentation for these useful:  
[https://en.cppreference.com/w/cpp/language/static\_cast](https://en.cppreference.com/w/cpp/language/static_cast)  
[https://en.cppreference.com/w/cpp/language/reinterpret\_cast](https://en.cppreference.com/w/cpp/language/reinterpret_cast)

The purpose of the `reinterpret_cast` in the `key_callback ` example is to cast from the `void*` pointer to a pointer of type `MyWindowHandler*`. This is effectively saying that the compiler should treat the memory pointed to by the pointer as being an instance (object/variable) of type MyWindowHandler.

In this case, you could also use `static_cast` as `void*` can be cast to any pointer whose object has the same alignment rules (if the original object was constructed as a MyWindowHandler then it will work).

So the following works:

```auto
void* pVoid;
int* pInt = static_cast<int*>(pVoid);

```

as does:

```auto
void* pVoid;
int* pInt = reinterpret_cast<int*>(pVoid);

```

You can also do:

```auto
float* pFloat;
int* pInt = reinterpret_cast<int*>(pFloat);

```

But you can’t do:

```auto
float* pFloat;
int* pInt = static_cast<int*>(pFloat); // error does not compile

```
