'What is the best way to pass parameters to a callback function? (C/C++) [closed]
I'm learning SDL right now and I'm trying to use timer callback function.
The function SDL_AddTimer() offer me only one argument to pass through but I want to pass different types of variables into it. (https://wiki.libsdl.org/SDL_AddTimer)
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Thank you for your help ~~
Solution 1:[1]
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Yes this is the correct and best way to pass parameters whenever you are presented with a callback that accepts void*
. Then cast back to the struct type from inside the callback.
Same thing goes when using pthreads/Windows threads or other such common APIs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lundin |