Path: utzoo!attcan!uunet!husc6!bloom-beacon!tut.cis.ohio-state.edu!rutgers!mcnc!thorin!coggins!coggins From: coggins@coggins.cs.unc.edu (Dr. James Coggins) Newsgroups: comp.lang.c++ Subject: Re: Emulate elementsPerform:with: method in C++ Summary: Here's a solution Message-ID: <4645@thorin.cs.unc.edu> Date: 12 Oct 88 00:00:24 GMT References: <3287@tekig4.TEK.COM> Sender: news@thorin.cs.unc.edu Reply-To: coggins@coggins.UUCP (Dr. James Coggins) Distribution: na Organization: University Of North Carolina, Chapel Hill Lines: 33 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.