Re: [gtk-list] Unidentified subject!



On Mon, 20 Dec 1999 13:52:51 -0500, Peter Garner wrote:
> 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": 

I think you're confused by the C++ name mangling. C++ uses exactly the
same calling convention as C (given that both compilers come from the same
"manufacturor").

> //  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. 

The matching problem is a name mangling problem (which will not occur in
this example).

> 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. 

There is no guarantee, but there is also no reason for the gcc maintainers
to change the calling convention. Given the fact that the C and C++
compilers use the same code generation algorithms it will be pretty hard
to change. I'm over two and a half years subscribed to the gcc mailinglist
(and its precursors gcc2 and egcs) and I've never seen a message about
changing the calling convention *between* the C and C++ compiler. The only
changes in calling convention are to make it compatible with the vendor OS
ABI.

What does the C++ standard have to say about this? 


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/





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