C ++ function pointer

zhaozj2021-02-16  41

C

Function pointer

Let's take a look at this code:

#include

Using std :: cout;

Using std :: end1;

Inline Int min (int A, int b)

{

RETURN (A> b)? b: a;

}

INT min (int A, int b, int (* pf) (int, int))

// You can use the default parameters: int Min (int A, int b, int (= int, int) = min)

{

Return Pf (a, b); // Call the function through the function pointer, or write as

// Return (* PF) (A, B); the function is the same.

}

Int main (int Argc, char * argv [])

{

INT i = 1;

INT j = 10;

INT r = min (i, j, min); // If you use default parameters, you can write: int R = min (i, j);

Cout << r << Endl;

Return 0;

}

Where INT (* pf) (int, int) defines a function pointer for the return value of INT and the parameter is two int. If it is not included in * PF, ie:

INT * PF (int, int),

The compiler will explain it to a return value as an integer pointer, and the parameter is a function of two int.

You can simplify the code with TypeDef:

#include

Using std :: cout;

Using std :: end1;

TypeDef int (* pf) (int, int);

// This line code is the key, quite stating the function pointer in the previous example as a data type.

Inline Int min (int A, int b)

{

RETURN (A> b)? b: a;

}

INT MIN (int A, int b, pf f) // pf f defines F as a function pointer as in the previous example.

{

Return F (A, B);

}

Int main (int Argc, char * argv [])

{

INT i = 1;

INT j = 10;

INT r = min (i, j, min);

Cout << r << Endl;

Return 0;

}

You can also provide a function pointer implemented with a template:

#include

Using std :: cout;

Using std :: end1;

Inline Int min (int A, int b)

{

RETURN (A> b)? b: a;

}

Template

Tmin (T A, T B, T (* PF) (T, T))

{

Return PF (A, B);

}

Int main (int Argc, char * argv [])

{

INT i = 1;

INT j = 10;

INT R = MIN (i, j, min);

// int R = min (i, j, min ); this form of compiler will report an error: Expression Syntax

Cout << r << Endl;

Return 0;

}

Of course, this pointer points to the function can also be implemented by template:

#include

Using std :: cout;

Using std :: end1;

Template

Inline T MIN (T A, T B)

{

RETURN (A> b)? b: a;}

Template

Tmin (T A, T B, T (* PF) (T, T))

{

Return PF (A, B);

}

Int main (int Argc, char * argv [])

{

Long i = 2000000;

Long J = 1000000;

// There are three forms:

Long r = min (i, j, min );

//The first. Note that you must add after min, otherwise the compiler will report an error:

// Could Not Find A Match for "MIN (long, long, t (*) (t, t)"

// Second: long r = min (i, j, min);

// Third: long r = min (i, j, min );

// is the same.

Cout << r << Endl;

Return 0;

}

However, I can't use Typedef to make the code easier, just like the following form:

Template

TYPEDEF T (* PF) (T, T);

Compiler will prompt: Templates Must Be Classes or functions

You can also use the array of function pointers:

#include

Using std :: cout;

Using std :: end1;

Inline Int min (int A, int b)

{

RETURN (A> b)? b: a;

}

Inline Int Max (Int A, INT B)

{

RETURN (a> b)? a: b;

}

Int main (int Argc, char * argv [])

{

INT i = 1;

INT j = 10;

Int (* pf [2]) (int, int);

// With a function of the function of two elements, each element is the return value INT, the parameter is a function pointer for two int.

PF [0] = min;

PF [1] = max;

INT R1 = PF [0] (i, j);

INT R2 = PF [1] (i, j);

Cout << r1 << endl;

Cout << r2 << endl;

Return 0;

}

Pointer to the overload function is also worth noting: #include using std :: cout; using std :: end1

Inline void print (int a) {cout << a << end1;} inline void print (long b) {cout << b << endl;}

INT Main (int Argc, char * argv []) {INT i = 1; long m = 100000;

Void (* pf1) (int) = print; void (* pf2) (long) = print

PF1 (I); PF2 (M);

RETURN 0;} The program is running very successful. Because the compiler automatically looks for all overload functions to find and function pointer points to function with the same return type and parameter table functions.

As we can know the general rules of a function pointer to a given function: that is, the return type of this function pointer and the parameter table must be the same as the given function. Note that the omitted number is also part of the function type, INT Function1 (int, ...) and int function 2 (int) requires two different function pointers. In fact, the function name is a pointer to this function. For Int Function (int), Function is its pointer. We can initialize the function pointer with this feature: int (* pf) (int) = function;

Take the address operator can also be used on the function name, the above code and int (* pf) (int) = & function) are the same.

(All code is debugged in C Builder6)

转载请注明原文地址:https://www.9cbs.com/read-26886.html

New Post(0)