You can't use normal non-static C++ class member functions as callbacks because there's an invisible parameter (this) required on the stack when calling such functions. Windows is fundamentally C based, and doesn't know anything about objects and "this" pointers, so the callbacks don't use them. In any case, how would the OS know what "this" to use, and what kind of "this" to point at ?
You can of course use a static member function or a simple global function as a callback, because they're not associated with a specific class instance, so no "this" is used when calling them. But hang on, what do you then do if you want to access class member variables or methods? A static member function can't access a specific instance's members. Well, you have a number of options:
If you only ever have one object of this class, you can simply stick "this" in a global and dereference through that global pointer in your callback function to get to the class object. But if you can have more than one instance, you need some way of mapping back to the correct value of this in the callback function, so read on... |
Some API functions which use callbacks allow you to specify user-defined data which is then supplied back to your callback, so you can put a copy of "this" in that data and dereference it in your callback function. The multimedia timer function timeSetEvent is one example of such an API. |
If the API you're using doesn't support user defined data, you'll have to invent your own mapping scheme using some other unique data (such as a window handle) to get back to "this". Every program is different in this respect, so I can't advise on the details. |