Re: [gtk-list] callbacks and classes



//I am defining callbacks in the menu factory, and i want to declare the
//callbacks as members of a class. (tClass.ButtonCallback) for example,
//however these functions must be declared static in order for them
//to be handled as callbacks....otherwise it complains that I am calling
//the function with (tClass::) instead of void(*)...(and i cant cast it)
//
//I dont want to have to declare the function static, because then I 
//will have to declare all member variables etc of my class as static as
//well (the instance of tClass isnt seen from the class definition file)...
//
//whats my best way around this ? 

<dry theory>
To call non-static member functions of a class, a `this' pointer is needed, as 
this is passed as an implicit first parameter to such a function. (This pointer 
is usually provided by either (a) calling from another non-static member 
function of the object, in which case the same `this' is used, or (b) by doing 
something like some_object->my_function(), where some_object functions as 
`this'.) This `this' is needed to access the data members of the object and 
possible virtual functions, which are stored somewhere around where the `this' 
pointer points.
</dry theory>

What has worked for me, so far, is to make a static member function, and give 
it a pointer to your object as func_data (i.e. gtk_signal_connect( my_button, 
"clicked", my_static_member_func, this ); ). Then, also make a non-static 
member function, which will do the real work, and from your 
my_static_member_func, do something like this: ((MyClass *)func_data)->
my_normal_member_func( ... ).

The problem, of course, is that this `eats up' your `free' func_data pointer. 
If you want to pass more parameters, you'll need to declare some struct, put 
your object and other pointers in there, then pass that.

//thanks

Well, hope it helps. :)

//Jeff

Johannes.
--
Visually inspecting visual programming languages.

Time is money, but the converse does not hold.




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