简体   繁体   中英

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:

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?

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

how can *pf() and pf() be the same where pf is a pointer to the 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. Specifically, the rules say:

[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.

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. In both cases the function pointed by pf is called.

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

The primary difference is that function pointers point to functions, and pointers to objects point to objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM