Does your compiler complain every time you pass a method as a callback function to a Carbon API, even though you're sure the method's signature is OK?
Depending on what you need, you actually can. It's just not necessarily as simple as installing MacOS. Here are the situations you may have and their solutions:
1) If you have a C++ class with a static member function and want to use that as a callback, you can. There is no problem with that, as long as you don't leave out any little "const" or otherwise choose the wrong data type for one of the parameters.
2) If you have a C++ class with a non-static or even virtual member function, you can't directly use it as a callback. The reason for this is that such functions depend on one particular object they belong to (if they don't, they should be static), and MacOS has no way of carrying along the name of the object the function belongs to (C++ internally passes an additional parameter with the "this" pointer, but every compiler does this differently and thus Apple can't rely on it).
However, with a little trickery, you can help MacOS. Simply declare a static function to be the callback function in your class. Then, when installing your callback, pass a pointer to the object in the "refCon" or "userData" parameter that most callbacks have. Now, all your callback has to do is to extract the object pointer from the userdata, cast it to the right type and call its virtual callback function.
Why can't I use my C++ member function as a callback (e.g. event handler, action proc)? | 1 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Why can't I use my C++ member function as a callback (e.g. event handler, action proc)?
Authored by: Anonymous on
Friday, June 04 2004 @ 05:49 PM BST
To see an example of this technique in action, Look in the /Developer/
Examples/Carbon/HIFramework sample provided as part of the standard
developer tools installation.
The technique is embodied in the way that TObject handles it's Carbon
Events. It registers a single static callback, TObject::ObjectEventHandler,
as the handler for the carbon events that are sent to the object and when
that item is registered, it also passes the "this" pointer as the user data.
In the static handler, it typecasts the user data back to a TObject pointer
and uses that pointer to call methods in the TObject based on which event
is being handled.
Examples/Carbon/HIFramework sample provided as part of the standard
developer tools installation.
The technique is embodied in the way that TObject handles it's Carbon
Events. It registers a single static callback, TObject::ObjectEventHandler,
as the handler for the carbon events that are sent to the object and when
that item is registered, it also passes the "this" pointer as the user data.
In the static handler, it typecasts the user data back to a TObject pointer
and uses that pointer to call methods in the TObject based on which event
is being handled.