beautypg.com

Apply functions – HP Integrity NonStop J-Series User Manual

Page 148

background image

Apply Functions

The second kind of user-defined function is an apply function. Its general form is:

void yourApplyFunction(type* ty, void* a)

where yourApplyFunction is the name of the function, and type is the type of the members of
the collection. Apply functions give you the opportunity to perform some operation on each
member of a collection, perhaps print it out or draw it on a screen. The second argument is
designed to hold client data to be used by the function, perhaps the handle of a window on
which the object is to be drawn.

In the following example, the apply function printAFoo is used to print out the value of each
member in

RWGDlist(type)

, a generic doubly-linked list:

#include
#include
class Foo {
public:
int val;
Foo(int i) {val = i;}
};
declare(RWGDlist, Foo)
void printAFoo(Foo* ty, void* sp){
ostream* s = (ostream*)sp;
(*s) << ty->val << "\n";
}

main(){
RWGDlist(Foo) gd;
gd.append(new Foo(1));
gd.append(new Foo(2));
gd.append(new Foo(3));
gd.append(new Foo(4));
gd.apply(printAFoo, &cout);
while(!gd.isEmpty())
delete gd.get();

return 0;
}

Program Output:

1
2
3
4

This manual is related to the following products: