Re: [gtk-list] gtk_signal_connect incompatible with C++?



Hello.
	This can´t be call an incompatibility. gtk_signal_connect expects a
function and you give him a method (an object function). Yes every
method has a hidden this attributte, which is defined in the languaje
and you can use inside your code.
	The solution. Functions to be passed to gtk_signal_connect must be non
object´s function or static object´s function. If you define a method as
static it can read/write static members in the class, and cant´t
read/write normal members of which there are a copy in each instance or
object. Due to this static method doesn´t have this argument.

	You can use the data argument to pass a pointer to the object to allow
the static function manipulate the object or call non static object
methods.
	As example:

// This mus be defined as static in class definition
void ObjectType::st_response_method(GtkWidget *widget, ObjectType
*object)
{
	object->response_method(widget);
}

void ObjectType::response_method(GtkWidget *widget)
{
	// Here you can access member fields and methods
}


	Bye.
Harvey Devoe Thornburg wrote:
> 
> Hi, I'm new to the list, and I'm pretty new to GTK, reasonably new to C++.
> I saw a few posts in the archives about this but none seem to point
> towards the kind of solution I'm looking for.  Anyway, the problem is I
> have a class called Panel, and one of its member functions is an
> adjustment callback. Inside one of the other Panel member functions, I
> connect the adjustment to the callback via this piece of code:
> 
>  gtk_signal_connect(GTK_OBJECT(KnobData[id]),
>          "value_changed", GTK_SIGNAL_FUNC(knobAdjustCallback),
>          pIds+id);
> 
> So KnobData[id] contains the pointer to the adjustment, knobAdjustCallback
> is the callback, and pIds+id is a pointer to an int16 (short) which is the
> id for the particular knob associated with the adjustment.
> 
> The callback declaration is:
> 
> void Panel::knobAdjustCallback(GtkAdjustment *adj, int16 *pId)
> 
> Now the problem, which I discovered using gdb, is that there is a hidden
> first parameter in Panel::knobAdjustCallback, namely the this pointer.
> So when the callback is triggered, parameters come in out of alignment.
> The this pointer gets overwritten with the adj pointer, and the adj
> pointer gets overwritten with the pId pointer.  Meanwhile, the pId
> pointer points to garbage, so I end up with segmentation fault.
> 
> If there isn't an easy way around this, am I better off convincing the
> administrator to install GTK-- (which I hope *is* compatible with C++)?
> Doing things like defining the callback outside the class, etc. isn't
> really an option (although it "fixes" the problem). If there's a patch
> underway that addresses this problem, please let me know.
> 
> Thanks,
> --Harvey
> 

-- 
+--------------------------+---------------------------+
|David Abilleira Freijeiro | Pontevedra, España        |
+--------------------------+---------------------------+
|http://members.xoom.com/odaf   (mailto:odaf@nexo.es)  |
|    (UNED, Mis Programas, Linux, Programación, etc.)  |
|WAINU: http://www.geocities.com/Athens/Forum/5889/    |
+------------------------------------------------------+



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