From owner-freebsd-hackers Tue Oct 26 20:39:56 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from chai.torrentnet.com (chai.torrentnet.com [198.78.51.73]) by hub.freebsd.org (Postfix) with ESMTP id 5B26414E5C for ; Tue, 26 Oct 1999 20:39:34 -0700 (PDT) (envelope-from bakul@torrentnet.com) Received: from chai.torrentnet.com (localhost [127.0.0.1]) by chai.torrentnet.com (8.8.8/8.8.5) with ESMTP id XAA11567; Tue, 26 Oct 1999 23:39:24 -0400 (EDT) Message-Id: <199910270339.XAA11567@chai.torrentnet.com> To: Chuck Robey Cc: tbuswell@acadia.net, Thomas David Rivers , freebsd-hackers@FreeBSD.ORG Subject: Re: X11/C++ question In-reply-to: Your message of "Tue, 26 Oct 1999 22:54:15 EDT." Date: Tue, 26 Oct 1999 23:39:24 -0400 From: Bakul Shah Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Allow me add something to what the FAQ-Xt says. I find it more convenient to immediately call a non-static function as shown below (using a slightly modified example from the FAQ). class Icon { public: Icon(Widget*); private: static void static_callback(Icon*); inline void real_callback(); Widget* button; int count; ... }; Icon::Icon(Widget* parent): count(0) { button = XtVaCreateWidget("Boo!", xmPushButtonWidgetClass, parent, 0); XtAddCallback(button, XmNselectCallback, &Icon::static_callback, (XtPointer)this); ... } inline void Icon::real_callback() { count++; ... } void Icon::static_callback(Icon* icon) { icon->real_callback(); } The reason for calling real_callback from static_callback is to avoid having to specify icon->count etc. to reference components of the Icon object. This makes a difference in readability if your callback function does a bunch of stuff. The reason for inlining to not incur the cost of an additional call (if this matters -- usually it doesn't). Inlining can get in your way during debugging (atleast gdb loses its mind) so provide a way to disable it. BTW, this idiom is useful pretty much any time you have to use C++ callbacks from C code, not just for X11. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message