From: function pointers
What is a function pointer?
A function pointer points to a special data type, the type of the function is determined by the data type it returns and its parameter list, and the name of the function is not part of its type.
The name of a concrete function, if not followed by a call symbol (that is, parentheses), then the name is a pointer to the function (note: in most cases, this can be considered, but it is not very strict).
How to declare function pointer
// Define the function pointer pf
int (*pf)(constint&, const int&); (1)
The above pf is a function pointer that points to all functions whose return type is int and takes two const int& parameters. Note that the parentheses around *pf are required, otherwise the above definition becomes:
int *pf(const int&, const int&); (2)
And this declares a function pf with return type int * with two const int& parameters.
Defining function pointer types with typedef
// Define the function pointer type cmpFun
typedef int (*cmpFun)(constint&, const int&); (3)
In this way, cmpFun becomes a data type that can be used to declare and define function pointers in the form of pf in (1), for example:
cmpFun pf = 0;
cmpFun pf = someFunction;
Take an example to illustrate:
- #include <iostream>
- #include <string>
- usingnamespace std;
- // define the function pointer pf
- int (*pf)(constint&, constint&);
- // Define the function pointer type cmpFun
- typedefint (*cmpFun)(constint&, constint&);
- // concrete function
- int intCompare(constint& aInt, constint& bInt)
- {
- if(aInt == bInt) return 0;
- if(aInt > bInt)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- int main(void)
- {
- int aInt = 1;
- int bInt = 2;
- pf = intCompare;
- // pf = &stringCompare; // exactly the same as above
- // use pf
- if(pf(aInt, bInt) == 0)
- {
- cout << "two integers are equal" << "." << endl;
- }
- elseif(pf(aInt, bInt) > 0)
- {
- cout << aInt << " is greater than " << bInt << "." << endl;
- }
- else
- {
- cout << aInt << " is less than " << bInt << "." << endl;
- }
- cout << "------------------------" << endl;
- // Declare and initialize a function pointer pf2 with function pointer type cmpFun
- cmpFun pf2 = intCompare;
- // use pf2
- if(pf2(aInt, bInt) == 0)
- {
- cout << "two integers are equal" << "." << endl;
- }
- elseif(pf(aInt, bInt) > 0)
- {
- cout << aInt << " is greater than " << bInt << "." << endl;
- }
- else
- {
- cout << aInt << " is less than " << bInt << "." << endl;
- }
- return 0;
- }
function pointer as parameter
A function pointer can be used as a parameter to a function. There are two ways to do this:
(a) int plusFun(int&,int&, int (constint&, const int&));
(b) int plusFun(int&,int(*)(const int&, const int&));
The above two methods do similar things: the third parameter of the plusFun function in (a) is a function pointer, and the second parameter in (b) is also a function pointer. Below we define the two plusFun functions declared earlier.
The plusFun in (a) is defined as follows:
- // function pointer as parameter: bad practice
- //int plusFun(int& aInt, int& bInt, int paf(const int& cInt, const int& dInt))
- //{
- //
- // return aInt + bInt + paf (cInt, dInt);
- //}
- // function pointer as parameter: the right way to do it
- int plusFun(int& aInt, int& bInt, int paf(constint &, constint &))
- {
- int cInt = 2;
- int dInt = 1;
- return aInt + bInt + paf (cInt, dInt);
- }
- The code to call plusFun:
- …
- pf = intCompare;
- …
- // function pointer as parameter
- int aaInt = 3;
- int bbInt = 4;
- cout << plusFun(aaInt, bbInt, pf) << endl;
- The plusFun in (b) is defined as follows:
- // function pointer as parameter: bad practice
- //int plusFun(int& aInt, int(*paf2)(const int& bInt, const int& cInt))
- //{
- // return aInt + paf2(bInt, cInt);
- //}
- // function pointer as parameter: the right way to do it
- int plusFun(int& aInt, int(*paf2)(constint&, constint&))
- {
- int bInt = 1;
- int cInt = 2;
- return aInt + paf2(bInt, cInt);
- }
- The code to call plusFun:
- …
- cmpFun pf2 = intCompare;
- …
- // function pointer as parameter
- int aaInt = 3;
- cout << plusFun(aaInt, pf2) << endl;
function pointer as return value
The return value of a function can be a function pointer. This declaration form is a bit cumbersome to write:
// function pointer as return value
int (*retFunPointer(int))(constint&, const int&);
The meaning of the above statement:
a) retFunPointer is a function that has a parameter of type int;
b) The return value of retFunPointer is a function pointer, which points to a function with two const int& type parameters and the return type is int.
Definition of retFunPointer:
// function pointer is the return value
int (*retFunPointer(int aInt))(constint&, const int&)
{
cout << aInt << endl;
// pf has been defined earlier
return pf;
}
Example of calling code:
// The function pointer is used as the return value, retFunPointer returns a function pointer of type cmpFun
cmpFun pf3 = retFunPointer(aaInt);
int result = pf3(aaInt, bbInt);
cout << result << endl;