Tools.h++ Manual
15-12 104011 Tandem Computers Incorporated
15
apply() functions
virtual void apply(RWapplyCollectable ap, void* x);
An efficient method for examining the members of a Smalltalk-like collection is 
member function 
apply()
. The first argument (
RWapplyCollectable
) is a 
typedef:
typedef void(*RWapplyCollectable)(RWCollectable*, void*);
that is, a pointer to a function with prototype:
void 
yourApplyFunction
(RWCollectable* item, void* x)
where yourApplyFunction is the name of the function. You must supply this 
function. It will be called for each item in the collection, in whatever order is 
appropriate for the collection, and passed a pointer to the item as its first 
argument. The second argument (
x
) is passed through from the call to 
apply() 
and is available for your use. It could be used, for example, to hold 
a handle to a window on which the object is to be drawn, etc.
Note – Notice the similarity to the 
apply() 
function of the generic collection 
classes (see “Apply functions” on page 8 in Chapter 14, ““Generic” Collection 
Classes”). The difference is in the type of the first argument of the user-
supplied function (
RWCollectable
* rather than type*). As with the generic 
classes, you must be careful that you cast the pointer item to the proper 
derived class.
The apply functions generally employ the "most efficient method" for 
examining all members of the collection. This is their great advantage. Their 
disadvantage is that they are slightly clumsy to use, requiring the user to 
supply a separate function.
1
1. The functional equivalent to 
apply() 
in the Smalltalk world is “do”. It takes just one argument—a piece of 
code to be evaluated for each item in the collection. This keeps the method and the block to be evaluated 
together in one place, resulting in cleaner code. As usual, the C++ approach is messier.










