c++ 函数指针应用,定义一个方法,传入两个参数和一个函数指针,并返回结果

#include <iostream>
#include <string>
using namespace std;
double add(double x, double y);
double calculate(double a, double b, double (*pf)(double, double));

int main() {
	double q = calculate(2.5, 10.4, add);
	cout << q << endl;
	return 0;
}

double add(double x, double y) {
	return x + y;
}

double calculate(double a, double b, double (*pf)(double, double)) {
	return (*pf)(a, b);
}

  

posted @ 2018-10-16 19:30  ranwuer  阅读(385)  评论(0编辑  收藏  举报