Re: Gtk widgets in gtkmm (Previously no subject)



On Monday 25 July 2005 07:32, Andrew E. Makeev wrote:
> Chris Vine wrote:
[snip]
> >g_signal_connect has its callback (slot) function passed as a C function
> >pointer (cast with G_CALLBACK()).
> >
> >The cludge (which will work with most but not all compilers) is to make
> > your callback a static member function of the class.  The correct
> > (standards complying) approach is to make the callback a function outside
> > the class with C linkage (ie declare it extern "C") and make it a friend
> > of the class if it needs access to the class's private or protected
> > members.
> >
> >You will need to pass the class instance to the callback if the callback
> > needs to access non-static data of the class.  If so pass the "this"
> > pointer as the last (data) argument to g_signal_connect(), and then cast
> > it back to the correct type in the callback.
>
> I only would add one note:
>
> you may wish to use "static" class method as callback for that signal,
> then you could save c++ style and avoid friend declarations.

I mentioned this as the "cludge" option above.  It is not standards 
conforming, as static class methods do not have C linkage and are therefore a 
separate type from functions (such as GSignal callbacks) which do:

Paragraph 7.5.1 of the standard:

"All function types, function names, and variable names have a language
linkage. The default language linkage of all function types, function names,
and variable names is C++ language linkage. Two function types with
different language linkages are distinct types even if they are otherwise
identical."

Static member functions work as C callbacks with g++ and many other compilers, 
but not with all.  Given the increasing pedantry of g++ in the gcc-4.0 
release, it would not surprise me if in due course g++ stops supporting it.

Chris.



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