最近在学习指针函数时遇到一个问题,我先上代码:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
double besty(int);
double pam(int);
void estimate(int, double (* ptr)(int));//double (*ptr)(int) 代表的是指向函数地址的指针
int main() {
int code;
cout << "How many llines of code do you need ?" << endl;
cin >> code;
cout << "Here's Besty's estimate :\n";
//将函数作为参数传递时,传递函数的地址时仅需要将函数名传递就可以
estimate(code, besty);
cout << "Here's Pam's estimate :\n";
estimate(code, pam);
return 0;
}
double besty(int line) {
return 0.05 * line;
}
double pam(int line) {
return 0.03 * line + 0.0004 * line * line;
}
void estimate(int lines, double (* ptr)(int)) {
cout << lines << " lines will take ";
cout << (*ptr)(lines) << " hour(s)\n";
}
指针函数作为形参这个知识点我是能看懂的,但是之后我就在想一个问题,
void estimate(int, double (* ptr)(int));
这个函数的参数列表中有函数地址作为参数并使用指针指向这个地址,那么这个指针可以用const修饰吗,如果可以,函数原型应该怎么写,然后有需要注意的地方吗?求各路大佬指点迷津!!!