<分区>
我正在学习函数指针和来自 wiki 的这个例子:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
int main()
{
int a, b;
int (*plus)(int, int) = add;
a = operation(7, 5, plus);
b = operation(20, a, subtract);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
如我所见,plus
是指向函数 add
的指针,它被传递给函数操作。很明显。但是 subtract
呢?
为什么不使用指针呢? 2种方法有什么区别?它是 c++
特定的吗?