//指针函数
int *f(int x, int y){} //pointer function
//f is a function, it returns a pointer
//函数指针
int (*f)(int x, int y){} //function pointer
//f is a pointer, it points to a function
程序示例
//函数指针的使用
#include <iostream>
using namespace std;//使用命名空间
//未使用 const
double triangle_area(double &x,double &y){
return x*y*0.5;
}
double rectangle_area(double &x,double &y){
return x*y;
}
double print_area(double (*p)(double &,double &),double &x,double &y){
return p(x,y);
}
/*
//使用const后
double triangle_area(double const &x,double const &y){
return x*y*0.5;
}
double rectangle_area(double const &x,double const &y){
return x*y;
}
*/
double print_area(double (*p)(double const&,double const&),double &x,double &y){
return p(x,y);
}
int main(){
double a=2,b=3;
double (*p)(double &,double &);//定义函数指针
p=triangle_area;//函数指针指向其中一个函数
cout<<"三角形的面积为:"<<print_area(p,a,b)<<endl;
p=rectangle_area;//函数指针指向另一个函数
cout<<"矩形的面积为:"<<print_area(p,a,b)<<endl;
return 0;
}