[comp.lang.c++] Emulate elementsPerform:with: method in C++

jimst@tekig4.TEK.COM (Jim Stanley) (10/07/88)

I would like to send a message to each element on a list of objects.
I'd also like the message to be an argument that I give to the routine
that does the sending.  This emulates the action of the Objective-C
elementsPerform:with: method.  So far, I can't see a way to do this
using, for instance, pointers to member functions.

Does anyone know of a way to do this in C++?  Thanks.
-- 
uucp:	    {ucbvax,decvax,ihnp4,allegra,uw-beaver}!tektronix!tekig4!jimst
US Mail:    James Stanley, Lab Instruments Engineering, Tektronix, Inc.
	    Box 500  MS 39-140, Beaverton OR 97077
Phone:	    503-627-3073, 627-3080

coggins@coggins.cs.unc.edu (Dr. James Coggins) (10/12/88)

In article <3287@tekig4.TEK.COM> jimst@tekig4.TEK.COM (Jim Stanley) writes:
>I would like to send a message to each element on a list of objects.
>I'd also like the message to be an argument that I give to the routine
>that does the sending.  This emulates the action of the Objective-C
>elementsPerform:with: method.  So far, I can't see a way to do this
>using, for instance, pointers to member functions.
>
>Does anyone know of a way to do this in C++?  Thanks.

I have a list of graphical objects (globs), each of which has a name,
a current-transformation matrix, and a pointer to the next glob in the
list.  There are two kinds of globs: groups add to the definition of
glob a pointer to a contained glob; gobjects add to glob a pointer to
a displayable object (called a polyobject).  The entire hierarchy is
maintained through a glob* (that I will call tree) so messages to be
propagated to the hierarchy are sent through tree, as
    tree->transform(oname,T);
where oname is a char* for the name of the object(s) to be transformed
and T is a transformation matrix.  Transform is a virtual member function 
of glob and a member function of gobject and group.
The transform message looks like this:
        if this message is addressed to me,
          execute it on myself
        propagate the message onward to the next glob
        (the group version also propagates the message to the 
         contained glob)

This illustrates how a hierarchy can be created and how objects in the
hierarchy can be addressed from the root.  Regarding the rest of your 
question, I would prefer to avoid pointers to member functions and
provide instead a "dispatch" message that accepts a symbolic operation 
name and uses a nice simple switch statement to select the correct 
operation.