Article View: comp.windows.x
Article #38357Re: C++ and callbacks
From: Ian J Hogg
Date: Wed, 04 May 1994 19:46
Date: Wed, 04 May 1994 19:46
46 lines
2850 bytes
2850 bytes
In article <2q5f97INNt6o@eccdb1.pms.ford.com> poorman@cc4032.pms.ford.com (Glenn M. Poorman) writes:
>In article <pauld.767898877@sarge>, pauld@verdix.com (Paul Durbin) writes:
>|> I attempt to use a member function as callback but all three arguments (to the
>|> callback) are garbage. If I make it a friend then the arguments are correct,
>|> but then I can't get to the class data, without reference thru an instance of
>|> the class and I don't want the data to be static.
>|>
>|> So, what is causing this problem? Could I possibly use the *this pointer as
>|> the client_data to the callback, then cast to the appropriate class?
>
>The problem is that the first argument of any non-static member function in
>C++ is the "this" pointer. So if you try to register a member function
>as a callback with (i.e. Motif), it won't work because Motif doesn't know
>that the function it is calling is a member function and it will not pass
>the "this" pointer.
>
Your compiler should not have allowed you to pass a pointer to a member
function as the callback parameter. Pointers to member functions are not
compatible with pointers to functions.
Try compiling this little snip of code and see what happens:
#include <Xm/Xm.h>
class bogus {
public:
void my_callback(Widget, XtPointer, XtPointer);
};
int main(int, char **)
{
Widget w;
XtAddCallback(w, "my_callback", &bogus::my_callback, NULL);
}
All of my compilers give errors similar to:
CC: "testijh.C", line 12: error: bad argument 3 type for XtAddCallback(): void
(bogus::*)(Widget, XtPointer, XtPointer) ( XtCallbackProc expected) (1264)
PS. Just a nit, but it is Xt that handles callbacks not Motif.
--
===============================================================================
Ian Hogg ianhogg@cs.umn.edu
(612) 424-6332
Technical Details
Message-ID:
<CpAn01.5Dy@news.cis.umn.edu>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!hookup!swrinde!emory!europa.eng.gtefsd.com!MathWorks.Com!news.duke.edu!news-feed-1.peachnet.edu!umn.edu!milli!ianhoggReferences:
<pauld.767898877@sarge> <2q5f97INNt6o@eccdb1.pms.ford.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103734,ad5a39011a2d3d26 X-Google-Attributes: gid103734,public X-Google-Thread: 109fba,ad5a39011a2d3d26 X-Google-Attributes: gid109fba,public X-Google-Thread: 10ba33,ad5a39011a2d3d26 X-Google-Attributes: gid10ba33,public X-Google-ArrivalTime: 1994-05-04 13:08:23 PST Newsgroups: comp.windows.x,comp.lang.c++,gnu.g++.help Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!hookup!swrinde!emory!europa.eng.gtefsd.com!MathWorks.Com!news.duke.edu!news-feed-1.peachnet.edu!umn.edu!milli!ianhogg From: ianhogg@milli.cs.umn.edu (Ian J Hogg) Subject: Re: C++ and callbacks Message-ID: <CpAn01.5Dy@news.cis.umn.edu> Sender: news@news.cis.umn.edu (Usenet News Administration) Nntp-Posting-Host: milli.cs.umn.edu Organization: University of Minnesota, Minneapolis, CSci dept. References: <pauld.767898877@sarge> <2q5f97INNt6o@eccdb1.pms.ford.com> Date: Wed, 4 May 1994 19:46:01 GMT Lines: 46