Date: Tue, 26 Oct 1999 23:39:24 -0400 From: Bakul Shah <bakul@torrentnet.com> To: Chuck Robey <chuckr@picnic.mat.net> Cc: tbuswell@acadia.net, Thomas David Rivers <rivers@dignus.com>, freebsd-hackers@FreeBSD.ORG Subject: Re: X11/C++ question Message-ID: <199910270339.XAA11567@chai.torrentnet.com> In-Reply-To: Your message of "Tue, 26 Oct 1999 22:54:15 EDT." <Pine.BSF.4.10.9910262253040.29073-100000@picnic.mat.net>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199910270339.XAA11567>
