Detailed description of C ++ function pointers

Source: Internet
Author: User

1. Definition

Each function occupies a memory unit and has a starting address. the pointer to the function entry address is called a function pointer.

2. Syntax

The pointer variable to the function is generally defined:

Data Type (* pointer variable name) (parameter table );

3. Description

1) the data type in the definition form of the function pointer refers to the type of the return value of the function.

2) differentiate the following two statements:

INT (* p) (int A, int B); // P is a pointer variable pointing to a function. The Return Value Type of the function is integer.

Int * P (int A, int B); // P is the function name. The return value type of this function is an integer pointer.

3) the pointer variable pointing to the function does not necessarily point to which function, but only defines a variable of this type. It is used to store the function's entry address; if you assign the address of a function to a program, it points to the function.

4) when assigning values to function pointer variables, you only need to give the function name, instead of parameters.

For example, the prototype of function max is int.
Max (int x, int y); pointer P is defined as: int (* p) (int
A, int B); P = max; assigns the entry address of function max to the pointer Variable P. In this case, P is the pointer variable pointing to function max, that is, both P and max point to the beginning of function.

5) in a program, the pointer Variable P can point to different functions successively, however, a function cannot be assigned with an inconsistent function pointer (that is, a function pointer cannot be directed to a function of a different type ).

The following functions are available: int fn1 (int x, int y); int FN2 (int x );

Define the following function pointer: int (* P1) (int A, int B); int (* P2) (int );

Then

P1 = fn1; // correct

P2 = FN2; // correct

P1 = FN2; // compilation Error

6) After a function pointer is defined and directed to a function, the function can be called by the function name, you can also call the function by using the function pointer (that is, using the pointer variable pointing to the function ).

For example, the Statement C = (* p) (a, B); // indicates calling the function (max) pointed by P, and the real parameter is A, B, the function value obtained after the function call is completed is assigned to C.

7) The function pointer can only point to the function entrance, but cannot point to a certain instruction in the middle of the function. * (P + 1) cannot be used to represent the next instruction of a function.

8) one of the common purposes of function pointer variables is to pass pointers as parameters to other functions.

4. Example

Source code:

# Include <iostream> using namespace STD; # include <conio. h> int max (int x, int y); // calculates the maximum number of int min (int x, int y); // calculates the minimum number of int add (int x, int Y, int y); // sum void process (int I, Int J, INT (* p) (int A, int B); // apply the function pointer int main () {int X, Y; CIN> x> Y; cout <"Max is:"; process (X, Y, max); cout <"Min is: "; process (X, Y, min); cout <" add is: "; process (X, Y, add); getch (); Return 0 ;} int max (int x, int y) {return x> Y? X: Y;} int min (int x, int y) {return x> Y? Y: X;} int add (int x, int y) {return X + Y;} void process (int I, Int J, INT (* p) (int, int B) {cout <p (I, j) <Endl ;}

Output result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.