-=> Quoting Ben Curry to All
BC> Hi,
BC> is it possible to pass member functions as arguments to other
BC> functions?
Yes.
BC> I have a class called display which has a member function:
BC> void xGathercb(FL_OBJECT *, long);
So this is, more properly, display::xGathercb.
BC> I need to pass this function as an argument of another function:
BC> fl_set_object_callback(FL_OBJECT *, xGathercb, long);
Can't do it. It expects a C-style _function_, not a C++-style _member
function_.
BC> So it is possible to cast from:
BC> void (display::*)(blah)
BC> to
BC> void (*)(blah)
Nope. A void (display::*)(blah) is actually implemented either as void
(*)(display*, blah) or void (*)(blah, display*). As you can see, this is not
the same as void (*)(blah).
In other words, the fl_ library doesn't know which display object to call
hen
it is supposed to call xGathercb ... so it can't. Try making the callback
static, if possible. Otherwise, you'll have to redesign to allow the
allback
to be static.
Here's a question to ponder - can you set a variable in FL_OBJECT such as a
pointer? If so, try:
void xGathercb(FL_OBJECT* obj, long l)
{
void* p = fl_get_pointer(obj); // or whatever it is
// we know it's a display pointer!
display* pd = (display*)p; // or = static_cast(p)
pd->xGathercb(obj, l);
}
Also, in your display constructor:
display::display(...)
{
// ... set up your FL_OBJECT
fl_set_pointer(this); // put your display pointer into the object so you can
// retrieve it later.
// ... continue with function
}
This is your normal way of doing object-oriented classes (MFC, OWL, ICLUI,
whatever) in a C-based world. Oh, and I also recommend making the callback
function a static function of your class:
class display
{
public:
static void xGathercb(FL_OBJECT*, long);
private:
void _xGathercb(FL_OBJECT*, long); // can't use the same name
// ...
};
This would mean that your function definition would become:
void display::xGathercb(FL_OBJECT* obj, long l)
{
void* p = fl_get_pointer(obj); // or whatever it is
// we know it's a display pointer!
display* pd = (display*)p; // or = static_cast(p)
pd->_xGathercb(obj, l);
}
Hope this helps!
... This tagline printed on a recycled computer!
___ Blue Wave/OS2 v2.30
--- FastEcho 1.46
---------------
* Origin: House of Fire BBS - Toronto - (416)601-0085 - v.34 (1:250/536)
|