No Subject



>So you can always use the data argument as a pointer to your object. 

That works if you like :-) 

>The simplest strategy to use to wrap GTK widgets in C++ classes is to 
>use static members as callback functions, each having the "data" 
>member a pointer to the C++ object. The callback function will usually 
>invoke a method in the object. 

NOOOOOOOOOOOOO!!!!!:-) 

Do NOT use a static member method or ANY OTHER C++ method for a 
callback!  Especially with something like GTK where the method is cast 
forceably by a macro to the expected function pointer.  The problem is 
that C++ functions may or may not have the same calling convention, etc. 
as C functions.  The proper way to mix C and C++ function pointers is 
to declare the callback "extern C": 

//  gp is a pointer to a class of our own 
extern "C" gint callback ( GTKObject * pgtkO , gpointer gp ) 
{ 
  MyClass * pmclass = static_cast < MyClass * > ( gp ) ; 

  return pmclass -> doSomething ( pgtkO ) ; 
} 

By declaring the callback function as extern "C" you assure that there 
will be no matching problems between C and C++.  There was a HUGE 
discussion of this in comp.programming.threads a month or two ago. 
Indeed, not bothering to differentiate between C and C++ calling 
conventions SEEMS to work with current versions of g++, there is NO 
guarentee that it will work with future releases, and the resulting 
code is CERTAINLY not portable. 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]