zaterdag 23 juni 2007

Function Pointer, Pointer to Function.

To implement pointers to functions, you need to be able to do the following:
- Obtain the address of a function
- Declare a pointer to a function
- Use a pointer to a function to invoke the function

Obtaining the address of a function is simple, You just use the function name without trailing parentheses. That is, if think() is a function, then think is the address of the function.
Be sure you distinguish between passing the address of a function and passing the return value of a function:

process(think); // passes address of think() to process()
thought(think()); // passes return value of think() to thought()

Declaring a Pointer to a Function
The declaration of a pointer to a function should identify the function's return type and the function's signature (its argument list).
That is, the declaration of a pointer to a function should provide the same information about a function that a function prototype does. For example:

double pam(int); // prototype

Here's what a declaration of an approriate pointer type looks like:

double (*pf)(int); // pf points to a function that takes
// one int argument and that
// returns type double

Note that this looks just like the pam() declaration, with (*pf) playing the part of pam. Because pam is a function, so is (*pf). and if (*pf) is a function, then pf is a pointer to a function.

Tip:
In general, to declare a pointer to a particular kind of function, you can first write the prototype for a regular function if the desired kind, and then replace the function name with an expression in the form of (*pf). In this case, pf is a pointer to a function of that type.

The declaration requires the parentheses arround *pf to provide the proper operator precedence. Parentheses have a higher precedence than the * operator, so *pf(int) means pf() is a function that returns a pointer, whereas (*pf)(int) means pf is a pointer to a function:

double (*pf)(int); // pf points to a function that returns a double
double *pf(int); // pf() a function that returns a pointer-to-double

After you declare pf properly, you can assign to it the address of a matching function:

double pam(int);
double (*pf)(int);
pf = pam; // pf now points to the pam() function

Note that pam() has to match pf int both signature and return type. The compiler rejects non-matching assignments:

double ned(double);
int ted(int);
double (*pf)(int);
pf = ned; // invalid -- mismatched signature
pf = ted; // invalid -- mismatched return type

Example of a function declaration of a function that takes a function address as parameter:

void estimate(int lines, double (*pf)(int));

This declaration says the second argument is a pointer to a function that has an int argument and a double return value. To have estimate() use the pam() function, you pass pam()'s address to it:

estimate(50, pam); // function call telling estimate() to use pam()

Clearly, the tricky part about using pointers to functions is writing the prototypes, whereas passing the address is very simple.

Using a Pointer to Invoke a Function
Now we get to the final part of the technique, which is using a pointer to call the pointed-to-function. The clue comes in the pointer declaration. There, recall, (*pf) plays the same role as a function name. Thus, all you have to do is use (*pf) as if it were a function name:

double pam(int);
double (*pf)(int);
pf = pam; // pf now points to the pam() function
double x = pam(4); // call pam() using the function name
double y = (*pf)(5); // call pam() using the pointer pf

Actually, C++ also allows you to use pf as if it where a function name:
double y = pf(5); // also call pam() using the pointer pf

Using the first form is uglier, but provides a strong visual reminder that the code is using a function pointer.

/////////////////
Holy syntax! How can pf and (*pf) be equivalent?
a function name is an address concerned to C++ compiler,

func1(); to call it, where func1 is the address of func one, so it is dereferenced
automatically.

(*func1)(); dereference the function also, but now visible very much.

*func1(); will not dereference, but instead call func1() and then try to dereference the result, see this

* func1(); you see?

Geen opmerkingen: