///通常, 要声明指向特定类型的函数指针,可以首先编写这种函数的原型,然后用(*pf)替换函数名。
///这样pf就是这类函数的指针。
#include <iostream>
double betsy(int);
double pam(int);
using namespace std;
void estimate(int lines, double(*pf)(int));
int main()
{
int code;
cout << "How many lines of code do you need?";
cin >> code;
cout << "Here's Betsy's estimate: \n";
estimate(code, betsy);
cout << "Here's Pam's estimate: \n";
estimate(code, pam);
return 0;
}
double betsy(int lns)
{
return 0.05 * lns;
}
double pam(int lns)
{
return 0.03 * lns + 0.0004 * lns * lns;
}
void estimate(int lines, double(*pf)(int))
{
cout << lines << " lines will take ";
cout << (*pf)(lines) << " hour(s)\n";
}
/*
How many lines of code do you need?30
Here's Betsy's estimate:
30 lines will take 1.5 hour(s)
Here's Pam's estimate:
30 lines will take 1.26 hour(s)
Process returned 0 (0x0) execution time : 4.492 s
Press any key to continue.
*/
函数指针
最新推荐文章于 2024-09-01 18:56:57 发布