0

I have noticed that there are two methods to invoke a function pointer in C++.

Example code:

void A(int x){
    ...
    ...
}

main() {
    void (*f)(int);
    f=&A;
    f();       //Method 1
    (*f)();    //Method 2
}

Why do both Method 1 and 2 work? And what is the logic for both methods having the same behaviour?

Vishal
  • 3,178
  • 2
  • 34
  • 47

1 Answers1

1

They both work and there's no difference between them. You should use one of them, whichever you find more readable, (i recommend (*f) version because it implies that f is a pointer to a function), but whichever you choose, please use it consistently.

Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
  • Yes. (*f) in a way seems like de-referencing the function pointer, and then calling the function with appropriate arguements. But then why does a direct call like f() work? – Vishal Feb 20 '13 at 16:36