Re: [gtk-list] Help: Registering C++ Callback
- From: "David Given" <david_given tao co uk>
- To: gtk-list redhat com
- Subject: Re: [gtk-list] Help: Registering C++ Callback
- Date: Tue, 24 Nov 1998 16:39:16 +0000
>I'm having some problems using gtk+1.0.5 with g++. I'm trying to register
>C++ callback functions within my code. When I compile the program I get
>the following warning for all the callbacks I register with
>gtk_signal_connect:
[...]
>My program seems to run; the callbacks routines are called. However, the
>callback functions produce incorrect results when they accessing the
>class's data members:( Does this appear to be an error on my part or is
>there something special I should be doing in order to use C++ callbacks
>with gtk+?
Ah, yes. This is a standard problem with using C++ callbacks in a system based on C.
The problem is that a C++ member function has a hidden parameter, a pointer to the object. Most C++ compilers pass this first. So when GTK calls your callback, the parameters are all garbage.
What you need to do is to use a static member function. These, because they aren't attached to a specific instance, don't have this hidden parameter, so GTK can call them. Then put the instance pointer in the user parameter when you register the callback. Your static function can extract the instance pointer and call the appropriate method. Something like this (highly untested, excuse my code):
class Foo {
...
static gint cb_widget_wrapper(GtkWidget* widget, GdkEvent* event, gpointer data);
...
gint real_cb(GtkWidget* widget, GdkEvent* event);
};
...
gint Foo::cb_widget_wrapper(GtkWidget* widget, GdkEvent* event, gpointer data)
{
return ((Foo*)data)::real_cb(widget, event);
}
The warning is normal. If you think it looks ugly, cast the function pointer with (I think this is what it's called) GTK_FUNCTION.
gtk_signal_connect(GTK_OBJECT(obj), GTK_FUNCTION(&Foo::cb_widget_wrapper), (gpointer)self);
(It might be called GTK_CB_FUNCTION or GTK_SIG_FUNCTION or something like that.)
Alternatively, you could use VDK or GTK-- and avoid the whole problem completely.
--
+- David Given ----------------+
| Work: dg@tao.co.uk | All vacations and holidays create
| Play: dgiven@iname.com | problems, except for one's own.
+- http://wiredsoc.ml.org/~dg -+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]