ext.:http://www.cnblogs.com/youxin/archive/2012/04/16/2452620.html
One of the most common ways to pass functions as parameters is to define a function pointer with a typedef.
#include <iostream>UsingNamespaceStd;typedefint (*PF) (int,int< Span style= "color: #000000;" >); This approach is easiest to understand, defines a function pointer type, and the function name is a pointer. int f (PF p,int a,int< Span style= "color: #000000;" > b) {return P (A, b);} int Add (int a,int b) {return a+B;} int main () {cout<<f (Add, 1,2) <<endl; //3}
You can also do this:
#include <iostream>UsingNamespaceStd;typedefint Functype (Intint);//define Functype This function data type int f (functype p,int a, Int b)//Create a new functype p variable Here is the P front plus *, run as usual. {return P (A, b);} int Add (int a,int b) {return a+B;} int main () {cout<<f (Add, 1,2) <<endl; //3}
You can also do this:
#include <iostream>UsingNamespace Std; int F (int Add (int,< Span style= "color: #0000ff;" >int), int a,int b)// Unlike the second way, the direct use of the instance {return add (a, b);} int Add (int a,int b) {return a+B;} int main () {cout<<f (Add, 1,2) <<endl; //3}
C + + Basics-How to pass a function as a parameter