gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:33:02 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com