Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

comp.windows.x.intrinsics Frequently Asked Questions (FAQ)
Section - 28. How can I add a C++ member function as a widget callback?

( Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Business Photos and Profiles ]


Top Document: comp.windows.x.intrinsics Frequently Asked Questions (FAQ)
Previous Document: 27. What is and how can I implement drag and drop?
Next Document: 29.!How can I identify the children of a manager widget?
See reader questions & answers on this topic! - Help others by sharing your knowledge
----------------------------------------------------------------------
(Courtesy of lanzo@tekelec.com (Mark Lanzo), 7 Sep 1993)

You can not add a regular C++ member function as a callback to a 
widget, because the callback function won't be invoked with the
hidden object pointer "this" which all regular C++ member functions
require.  However, there is a way to get around this limitation:
"static" member functions.

A static member function is a function to which the usual scoping
and access rules apply, but which is not in fact actually associated
with a specific instance of an object, and it is NOT called using the 
special member function calling syntax.  In particular, there is no 
"this" pointer supplied to the function.  This means that a "static"
member function is really a lot like a "friend" function except that
its name is within the scope of the class (in other words, the name
is prefixed with the class name).

To demonstrate this, let's create a trivial class "Icon" which includes
a "button" widget, and a member function "select_CB" which is
installed as the "XmNselectCallback" on the button.  The class
declaration would then look something like this:

    class Icon
    {
    private:
            Widget      button;
            static void select_CB(Widget, XtPointer, XtPointer);

    public:
            Icon(Widget parent);        // Constructor
    };

For the example above, I can declare the constructor and callback
functions like this:
 
    Icon::Icon (Widget parent)
    {
	    button = XtVaCreateWidget ("icon_button", xmPushButtonWidgetClass,
				      parent, NULL);

	    //  Depending on your compiler, you may be able to specify
	    //  "&Icon::select_CB" simply as "select_CB" in the 
	    //  following statement:
	    XtAddCallback (button,  XmNselectCallback, &Icon::select_CB,
			  (XtPointer) this);
    }

    void Icon::select_CB (Widget w, XtPointer userData, XtPointer callData)
    {
	    Icon * icon = (Icon *) userData;        // Instead of "this"
 
            // do whatever you want to do when the icon is selected ...
    }

There are two things to note here:
 
 *) If you want the static member function to work on a specific 
    object, you must supply the object pointer (such as by "userData"
    in the example).  Just like a non-member function.
 
 *) Although the function is declared as "static" inside the class
    definition, you do NOT include the "static" keyword when you
    actually define the function.

User Contributions:

Comment about this article, ask questions, or add new information about this topic:

CAPTCHA