9.13 POINTERS TO FUNCTIONS IN C


9.13 POINTERS TO FUNCTIONS IN C++

Consider the following two declarations:

      int print(char*);      int (* fp)(char*) = &print;                         //(A) 

While the first declaration is a prototype of some function print(), the second declaration declares fp to be a variable that can serve as a pointer to a function that takes a char* argument and returns an int. The second declaration also initializes fp to point to the function print(). Since function names can also serve as pointers in some contexts, for the second declaration we could also have said

      int (*fp)(char*) = print;                                  //(B) 

Let's further say that the definition of print() is as follows:

      int print(char* str) {          cout << str << endl;          return strlen(str);      } 

We could now write a main() as follows:

      int main()      {          int n = fp("hello this is a test");          cout << "Number of characters printed: " << n << endl;      } 

Note that in this example in main(), the name fp is a global variable that can only be a pointer to a function. The function to which fp can point must meet the conditions laid out on the left hand side in the declaration at (A) above. The declarations in lines (A) and (B) also initialize fp to point to the function print().

Pointers to functions are useful in programs in which a function name is a variable. A classic example of this is the qsort function defined in the C standard library header <stdlib.h> for sorting an array of whatever:

      void qsort(void* base,                 size_t nmemb,                 size_t size,                 int (* compar) (const void*, const void*)); 

where the last parameter, compar, can be bound to any function of two parameters, both of type void*, that returns an int. Using a variable for a function name allows qsort() to be written in a most general form, so general that it can be used to sort ints, chars, strings, or any other object type, provided that you are able to supply a suitable comparison function for the parameter compar. (See Section 4.3.3 of Chapter 4 for a more complete presentation of this function.)

We will next show an example that illustrates how you can use a typedef for a function pointer (and therefore for a function name). The following statement

       typedef void (* FPT) (double); 

says that the identifier FPT will serve as a type definition for a pointer to a function that returns void and which has exactly one parameter of type double. Compare the syntax of this typedef with the more familiar one shown below:

       typedef int Bool; 

which tells the compiler that it should treat the type Bool as a synonym for the type int. By the same token, the previous definition says that wherever the compiler sees something declared as of type FPT, it should treat that as a function pointer (or as a function) which returns void and which takes exactly one argument of type double. Now we could say

       void foo(double d) { cout << "hello from foo"; }       FPT goo = foo; 

which first defines foo as a function that returns void and that takes one argument of type double. We then use this function to initialize goo which is of type FPT, meaning a function that returns void and that takes one argument of type double.

The following example illustrates the use of typedefs with pointers to function:

 
//Silly.cc #include <iostream> #include <string> class huphalumpusdumpusgumpus { public: string name; huphalumpusdumpusgumpus (string nam) : name(nam) {} }; typedef huphalumpusdumpusgumpus SILLY; void g(SILLY s) { cout << "silly is as silly does, says" << s.name << endl; } void (* FP) (SILLY) = g; typedef void (* FP_TYPE) (SILLY); FP_TYPE h(SILLY s) { cout << "silly, says function h" << endl; return g; } typedef void (* FPT) (double); void foo(double d) { cout << "hello from foo"; }] FPT goo = foo; int main() { SILLY s("Billy"); (*FP)(s); // silly is as silly does, says Billy FP(s); // silly is as silly does, says Billy h(s); // silly, says function h goo(3.0); // hello from foo return 0; }

The output produced by the program is shown in the commented out line endings in main.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net