c++ - C++中指向函数的指针,(*pf)() 和 pf 一样吗?

标签 c++ pointers

我是 C++ 的初学者,我在理解指向函数的指针时遇到了问题:

main()
{
    double pam(int);
    double (*pf)(int);
    pf = pam;            
    double x = pam(4);    
    double y = (*pf)(5); 
    double y = pf(5);
} 

(*pf)()pf() 怎么可能是一样的,其中 pf 是指向函数的指针?

其他指针和指向函数的指针有什么区别??

最佳答案

how can *pf() and pf() be the same where pf is a pointer to the function?

它们不相同(因为运算符优先级)。如果您的意思是 (*pf)()pf() 实际上是一样的,那是因为语言规则是这样说的。具体来说,规则说:

[expr.call]

A function call is a postfix expression followed by parentheses containing a possibly empty, comma-separated list of initializer-clauses which constitute the arguments to the function. The postfix expression shall have function type or function pointer type. For a call to a non-member function or to a static member function, the postfix expression shall either be an lvalue that refers to a function (in which case the function-to-pointer standard conversion ([conv.func]) is suppressed on the postfix expression), or have function pointer type.

(*pf)() 的情况下,表达式 (*pf) 是一个引用函数的左值,而在 的情况下pf(),表达式pf有一个函数指针类型。在这两种情况下,都会调用 pf 指向的函数。

What is the difference between other pointers and a pointer to the function??

主要区别在于函数指针指向函数,而对象指针指向对象。

关于c++ - C++中指向函数的指针,(*pf)() 和 pf 一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532376/

相关文章:

c++ - CreateFileMapping() 名称

c++ - 为什么 std::exception 析构函数不是 noexcept

c++ - 尝试使用 CUDA 分配内存时出现访问冲突写入位置错误

c++ - 限制指针算术或比较的基本原理是什么?

c++ - 为什么只有 char* 是 <Bad Ptr>,而不是其他数据类型?

c++ - std::string 获取 (char *) 而不是 (const char *)

C++ 指针 vector 如何影响性能?

C++如何检查Shell输入的数量

C++ 多线程 : terminate after throwing an instance of 'std::length_error'

c++ - 字符串是否到处都包含空子字符串?