Xref: utzoo comp.lang.c++:13662 comp.windows.open-look:1520 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!wuarchive!udel!rochester!kodak!uupsi!cmcl2!adm!lhc!lhc!warsaw From: warsaw@nlm.nih.gov (Barry A. Warsaw) Newsgroups: comp.lang.c++,comp.windows.open-look Subject: Re: Passing member functions Message-ID: Date: 24 May 91 19:08:14 GMT References: <1991May24.052811.10218@TIRS.oz.au> Sender: usenet@nlm.nih.gov (usenet news poster) Reply-To: warsaw@nlm.nih.gov Organization: Century Computing, Inc. Lines: 74 In-Reply-To: pete@TIRS.oz.au's message of 24 May 91 05:28:11 GMT >>>>> "Peter" == Peter Bartel writes: Peter> In the above case, callback_routine is an ordinary Peter> function; not a member of any class. My question is: how do Peter> I tell XView to use a member function as the panel notify Peter> procedure? What I want is something similar to the Peter> following (which doesn't work). [Example deleted for brevity] The problem with using a (non-static) member function for a callback is that it implicitly requires a `this' pointer, but the XView notifier can't put one on the stack before calling your member function. Fortunately, there's a very simple workaround (and this is what makes using XView with C++ fairly nice). When you're setting up the PANEL_NOTIFY_PROC, you want to use XV_KEY_DATA to attach the `this' pointer to the XView object. Next make the PANEL_NOTIFY_PROC a *static* member function (which does not require an implicit `this') and in this function, extract the key data placed on the object. Then use this object to call the member function that does the guts of your callback. Something similar to the following should do the trick. One caveat: I don't use this code fragment verbatim, but I do things along these lines. Just be aware that I haven't tested this exact example out, but it *should* work. ;-} Hope this helps. -Barry NAME: Barry A. Warsaw INET: warsaw@nlm.nih.gov TELE: (301) 496-1936 UUCP: uunet!nlm.nih.gov!warsaw ==================== example ==================== class my_class { public: void btn_set_callback( void ); private: void do_btn_callback( Panel_item item, Event* event ); static void btn_callback( Panel_item item, Event* event ); Panel_item btn; static Attr_attribute INSTANCE; }; Attr_attribute my_class::INSTANCE = xv_unique_key(); void my_class::btn_set_callback( void ) { xv_set( btn, PANEL_NOTIFY_PROC, &my_class::btn_callback, XV_KEY_DATA, INSTANCE, this, NULL ); }; void my_class::btn_callback( Panel_item item, Event* event ) { my_class* obj = (my_class*)xv_get( item, XV_KEY_DATA, INSTANCE ); obj->do_btn_callback( item, event ); }; void my_class::do_btn_callback( Panel_item, Event* event ) { // implicit `this' is now set correctly, so you can access any // member of my_class you want. };