函数本身可以作为形参吗?
最近在看数据结构的教程时,在线性表一章节发现有这么一个函数:LocateElem(LA,e,equal()),用于查找线性表LA中等于e的元素,equal()是比较函数,怎么成了形参了?函数也可以做形参吗?
[解决办法]
如果equal是个函数对象的话 就完全合法.
产生一个匿名对象.
[解决办法]
可以,跟函数指针是一样的~~~
[解决办法]
LocateElem(LA,e,equal()) ???
这里表示用equal()的返回值做参数。。ok?
[解决办法]
#include <iostream>using namespace std;void test(void f()){ f();}void f1(){ cout<<"f1"<<endl;}int main(){ test(f1); system("pause");}
[解决办法]
直接传递函数地址也是可以的。但你的例子不是。
比如:
void MyFun(int x); //这个申明也可写成:void MyFun( int );
void (*FunP)(int ); //也可申明成void(*FunP)(int x),但习惯上一般不这样。
int main(int argc, char* argv[])
{
MyFun(10); //这是直接调用MyFun函数
FunP=&MyFun; //将MyFun函数的地址赋给FunP变量
(*FunP)(20); //这是通过函数指针变量FunP来调用MyFun函数的。
}
void MyFun(int x) //这里定义一个MyFun函数
{
printf(“%d\n”,x);
}
参考:
http://blog.pfan.cn/whyhappy/6030.html
[解决办法]