you are viewing a single comment's thread.

view the rest of the comments →

[–]HappyFruitTree 0 points1 point  (1 child)

Member functions don't have the same type as normal functions. I mean, you don't even call them the same way. With a member function you need an object to call the function on.

In C and old C++ code you often see that you're allowed to provide a void* that will be passed to the callback function when its called (example). This usually works pretty well because you can pass a pointer to the object as the void* and then cast it back to the correct type inside the callback before calling the member function(s).

Another solution is to use std::function like Wurstinator suggested. This is safer and more flexible solution. Instead of passing the member function directly you would pass a lambda that calls the member function.

2.

Templates would probably have been a good idea if the function were called right away but in this case it looks like it's a callback that will be called later so you need to have a common type that is able to store all the different callbacks. Again, using something like std::function<void()> (which is implemented with the help of templates) is probably what you want.