兰州理工大学,研究生VC++课件6

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

• The general form of a declaration of a pointer to a function looks like this:
return_type (*pointer_name)(list_of_parameter_types);
• The declaration of a pointer to a function consists of three components: • The return type of the functions that can be pointed to • The pointer name preceded by an asterisk to indicate it is a pointer • The parameter types of the functions that can be pointed to
double (*pfun)(char*, int); // Pointer to function declaration
• This declares a pointer with the name pfun that can point to functions that accept two arguments, one of type pointer to char and another of type int, and return a value of type double.
• Clearly, this is going to restrict what you can store in a particular pointer to a function.
Declaring Pointers to Functions
• You can declare a pointer pfun that you can use to point to functions that take two arguments, of type char* and int, and return a value of type double like this:
6. More about Program Structure
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• • • • • • What a pointer to a function is How to define and use pointers to functions How to define and use arrays of pointers to functions What an exception is and how to write exception handlers that deal with them How to write multiple functions with a single name to handle different kinds of data automatically What function templates are and how to define and use them How to write a substantial program using several functions

• The parentheses around the pointer name, pfun, and the asterisk are essential; without them, the statement would be a function declaration rather than a pointer declaration. • In this case, it would look like this:
• Obviously, a pointer to a function must contain the memory address of the function that you want to call. • To work properly, the pointer must also maintain information about the parameter list for the function it points to, as well as the return type. • Therefore, the type for a pointer to a function must incorporate the parameter types and the return type of the functions to which it can point.
double *pfun(char*, int); // Prototype for a function, pfun, // returning type double*
• This is a prototype for a function pfun() that has two parameters, and returns a pointer to a double value.
long product(long, long); // Function prototype ... pfun = product; // Set pointer to function product()
Pointers to Functions
• To get a proper feeling for these newfangled pointers and how they perform in action, try one out in a program:
long sum(long num1, long num2); // Function prototype long (*pfun)(long, long) = sum; // Pointer to function points to sum()
• In general, you can set the pfun pointer that you declared here to point to any function that accepts two arguments of type long and returns a value of type long.
• • • • • • • • •
} // Function to multiply two values long product(long a, long b) {
return a*b;
} // Function to add two values long sum(long a, long b) {
• When you initialize a pointer to a function in the declaration, you can use the auto keyword for the type. • You can write the previous declaration as:
• • • • • • • • •
// EX06-01.cpp // Exercising pointers to functions #include <iostream> using std::cout; using std::endl; long sum(long a, long b); // Function prototype long product(long a, long b); // Function prototype int main(void) {
return a + b;
}
A Pointer to a Function as an Argument
• Because “pointer to a function” is a perfectly reasonable type, a function can also have a parameter that is of a pointer to a function type. • The function can then call the function pointed to by the argument. • The pointer can point to different functions in different circumstances, which allows the particular function that is to be called from inside a function to be determined in the calling program. • You can pass a function name explicitly as the argument for a parameter that is of a pointer to function type.
• You can initialize a pointer to a function with the name of a function within the declaration of the pointer. • The following is an example of this:
• You can also initialize a pointer to a function by using an assignment statement. • Assuming the pointer pfun has been declared as in the preceding code, you could set the value of the pointer to a different function with these statements:


POINTERS TO FUNCTIONS
• A pointer stores an address value that, up to now, has been the address of another variable. • This has provided considerable flexibility in allowing you to use different variables at different times through a single pointer. • A pointer can also point to the address of a function. • This enables you to call a function through a pointer, which will be the function at the address that was last assigned to the pointer.
long(*pdo_it)(long, long); // Pointer to function declaration pdo_it = product; cout << endl << "3*5 = " << pdo_it(3, 5); // Call product thru a pointer pdo_it = sum; // Reassign pointer to sum() cout << endl << "3*(4 + 5) + 6 = " << pdo_it(product(3, pdo_it(4, 5)), 6); // Call thru a pointer, // twice cout << endl<<endl; return 0;
• NOTE • The pointer can only point to functions with the same return_type and list_of_parameter_types specified in the declaration.
• If you attempt to assign a function to a pointer that does not conform to the types in the pointer declaration, the compiler generates an error message.
auto pfun = sum;
• As long as the prototype or definition for sum() precedes this statement in the source file, the compiler can work out the pointer type.
相关文档
最新文档