[英]pointer to the function in C++,are (*pf)() and pf same?
I'm a beginner in C++ and I have a problem understanding pointers to functions:我是 C++ 的初学者,我在理解函数指针时遇到问题:
main()
{
double pam(int);
double (*pf)(int);
pf = pam;
double x = pam(4);
double y = (*pf)(5);
double y = pf(5);
}
How can (*pf)()
and pf()
be the same, where pf
is a pointer to the function? (*pf)()
和pf()
怎么可能相同,其中pf
是指向 function 的指针?
What is the difference between other pointers and a pointer to the function??其他指针和指向 function 的指针有什么区别??
how can *pf() and pf() be the same where pf is a pointer to the function?
*pf() 和 pf() 如何在 pf 是指向 function 的指针的情况下相同?
They aren't the same (because of operator precedence).它们不一样(因为运算符优先级)。 In case you meant
(*pf)()
and pf()
are effectively the same, then that's because the rules of the language say so.如果您的意思是
(*pf)()
和pf()
实际上是相同的,那是因为语言规则是这样说的。 Specifically, the rules say:具体来说,规则说:
[expr.call]
[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.
function 调用是一个后缀表达式,后跟括号,其中包含可能为空的逗号分隔的初始化子句列表,这些初始化子句构成 arguments 到 ZC1C425268E68385D14AB5074C17A9。 The postfix expression shall have function type or function pointer type.
后缀表达式应具有 function 类型或 function 指针类型。 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.
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在后缀表达式上),或具有 function 指针类型。
In case of (*pf)()
, the expression (*pf)
is an lvalue that refers to a function, while in the case of pf()
, the expression pf
has a function pointer type.在
(*pf)()
的情况下,表达式(*pf)
是指向 function 的左值,而在pf()
的情况下,表达式pf
具有 function 指针类型。 In both cases the function pointed by pf
is called.在这两种情况下,都会调用
pf
指向的 function。
What is the difference between other pointers and a pointer to the function??
其他指针和指向 function 的指针有什么区别??
The primary difference is that function pointers point to functions, and pointers to objects point to objects.主要区别在于 function 指针指向函数,而指向对象的指针指向对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.