函数指针可以取消引用吗

Can function pointers be de referenced

摘录自 E Balagurusamy 的《C++ 面向对象编程》-

Using function pointers, we can allow a C++ program to select a function dynamically at run time. We can also pass a function as an argument to another function. Here, the function is passed as a pointer. The function pointer cannot be de-referenced. C++ also allows us to compare two function pointers.

这里写着函数指针不能解引用。但是下面的程序运行成功了。

#include<iostream> 
int Multiply(int i, int j) {
    return i*j;
}  
int main() {
int (*p)(int , int);
p = &Multiply;
int c = p(4,5);
int d = (*p)(4,11);
std::cout<<c<<" "<<d;
return 0;
}

在第四行的最后一行,我取消了对指针的引用。这是正确的吗?它没有给出任何编译器时间错误,而且倒数第 4 行中写的内容与倒数第 5 行中写的内容相同吗?我已经开始学习 C++,所以如果我问了一些非常愚蠢的问题,请不要介意。

你的编译器是对的,书是错的。

但是 p(4,5)(*p)(4,5) 做同样的事情,所以几乎不需要取消引用函数指针。

[expr.unary.op]/1

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type ...

(大胆的矿)


please don't mind if I have asked something really stupid

不,很好地验证了您阅读的内容。