我有两个问题
我看到了
int (*Ptr)(int,int);
Ptr=someOtherFuncion;
Run Code Online (Sandbox Code Playgroud)
它应该不是那样的吗?
Ptr=&someOtherFuncion;
Run Code Online (Sandbox Code Playgroud)
2.我学习了函数指针,就像那样回调
someOtherFunction(functionPointer)
Run Code Online (Sandbox Code Playgroud)
如果我将一个不是指针的常规函数放在什么区别?
函数的名称几乎立即衰减到指向函数的指针,因此someOtherFunction衰减到&someOtherFunction显式提供的同一指针.事实上,operator(&)地址的操作数是衰变不会发生的少数几个地方之一.
这有一个有趣的后果:即使您取消引用函数指针,它也会立即再次衰减.所以以下都是等价的:
someOtherFunction(1, 2);
(*someOtherFunction)(1, 2);
(**someOtherFunction)(1, 2);
(***someOtherFunction)(1, 2);
Run Code Online (Sandbox Code Playgroud)
所以,如果你感觉不适合分配一个没有显式地址的函数指针&,那么无论如何都要把它放在那里,但你不必这样做.
解决问题的第二部分:一个函数总是通过函数指针调用,但由于上面提到的即时衰减,可以像调用函数指针一样调用普通函数.