【发布时间】:2020-03-07 17:40:59
【问题描述】:
我知道为了将函数作为参数传递,我们需要将其设为函数指针。但是为什么我们需要在另一个函数中将函数指针作为参数传递,而我们可以在另一个函数定义中调用该函数呢?
//Why this
int (*f1)(int);
int testFunction(int (*f)(int));
//Instead of this
int f1(int);
int testFunction()
{
int f1Value = f1(2); //call the fucntion and store the value
}
这个例子可能没有任何意义。
什么时候我们倾向于传递函数指针而不是仅仅调用函数?
【问题讨论】:
-
您是否考虑过可以从其他地方为参数传递完全不同的指针的可能性?
-
@SamVarshavchik 我想我没有,我不确定你的意思。对不起
标签: c++ parameters function-pointers