From: Steve B. <sjb...@ai...> - 2004-04-23 14:12:00
|
jc...@tu... wrote: > I want to register the callback that is a method of a class, for example: > Is it possible to achieve what I want to do or should I stick with a > global display function. As other people have explained, you can't do this with a regular class member function - but you may be able to 'get away with it' if the function is static. Just to explain the reason for this: When you call a class member function, what actually happens at the machine-code level is that the address of the class object is passed as a 'hidden' first parameter to that function - and that first parameter becomes 'this' inside the function. When you pass the address of a function as a callback, the only thing that's passed is literally that - the address of the function. NOT the address of the object you were referring to. Hence there is no way to know (at the time the callback is invoked) what the address or type of the object was. All we know is the address of the function to call - with no way to know what the address of 'this' should be. Static member functions are just like normal global functions - you can't access 'this' or any non-static members of the class from within those kinds of functions. Hence there is no logical reason why you shouldn't pass those function addresses as callback functions. However, for whatever reasons, the C++ specification says that it's illegal?!? So, compilers that are being pedantic and following the spec to the letter don't allow it - those that are being pragmatic may let you get away with it - hopefully with a WARNING message. Personally, I think you should stick with using global functions for callbacks - that's the only fully portable way. ---------------------------- Steve Baker ------------------------- HomeEmail: <sjb...@ai...> WorkEmail: <sj...@li...> HomePage : http://www.sjbaker.org Projects : http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net http://prettypoly.sf.net -----BEGIN GEEK CODE BLOCK----- GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M- V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++ -----END GEEK CODE BLOCK----- |