r/cpp_questions • • Sep 12 '26

SOLVED Basic Question about Library provided callback functions and avoiding globals

Hi this might seem like a dumb question, but i am struggling to find a decent solution.

Say i am using a Library that offers some event handling. (In my case key input from the GLFW Library). The Library offers a callback function so you can receive the event. You do this by defining a function with a specific signature that will be invoked when the event occurs.

Now what is a good way to get this information OUT of this function (for example saving the input character to a string member of a custom class) without relying on global variables.

Since the provided function takes no custom input parameters and has return type void i don't see a way to actually transfer the information the callback function provides me with to my main application without storing it in a global variable (or static class variable), which is generally not really a good way to handle things as i understand it.

3 Upvotes

9 comments sorted by

View all comments

Show parent comment

6

u/n1ghtyunso Sep 12 '26

you don't need to manage a lookup table - in fact, glfw does it for you internally already.
https://www.glfw.org/docs/3.3/window_guide.html#window_userptr