foo();
(*foo)();
(&foo)();
What exactly is the difference between these function calls (assuming foo() is defined somewhere)? and are there any situations where one might be used over another?
Also, why don’t &foo() and *foo() work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no difference between the actual calls themselves (rather, they will all do the same thing depending on how
foo()is declared)All function calls in C and C++ take place via a function-pointer expression which appears before the function call parentheses. Implicit address-of of non-pointer types takes place if necessary.
Here’s an ideone demonstrating the behavior in C++.
The reason
&foo()and*foo()don’t work is that the function call operator()takes precedence over*and&. So they might work, depending on what you were doing with the return value.&foo()would take the return value’s address, and*foo()would dereference it. Under some circumstances, either of these operations, or both, might be legal. Consider a function returning a reference-to-pointer type.Part of this answer taken from R..’s comment.