#include <iostream>
using namespace std;
int fa(int a,int b)
{
return a+b;
}
int fb(int a,int b)
{
return a*b;
}
int fc(int a,int b)
{
return a-b;
}
int g(int a,int b, int(* f)(int a ,int b))
{
return f(a,b);
}
int main(int argc, char *argv[])
{
cout<<g(3,4, fa)<<endl;
cout<<g(3,4,fb)<<endl;
cout<<g(3,4,fc)<<endl;
return 0;
}

本文通过一个具体的 C++ 代码示例,展示了如何使用函数指针来调用不同的数学运算,如加法、乘法和减法。这个例子不仅解释了函数指针的基本概念,还演示了其在实际编程中的应用。
695

被折叠的 条评论
为什么被折叠?



