标题 c++函数指针的简单使用
在这里插入代码片
#include <iostream>
using namespace std;
double add(double x, double y) {
return x + y;
}
double cut(double x, double y) {
return x - y;
}
double * sum(double(*pf)(double, double),double(*pt)(double,double), double a, double b) {
double ans[2] = { pf(a, b), pt(a, b) };
double *an = &ans[0];
return an;
}//返回double类型的引用,形参利用了函数指针。
void main()
{
double a = 0, b = 0;
cout << "请输入a和b" << endl;
while (cin >> a >> b) //检测输入
{
double *ans=sum(add, cut, a, b);
cout << ans[0] << " " << ans[1] << endl;
cout << "请输入a和b" << endl;
}
cout << "end" << endl;
}