There are two ways to call a function in C ++:
By name:
f();
Via function pointer:
typedef void (*fptr_t)(); fptr_t fptr = &f; (*fptr)();
Now, using the operator address for the function name ( &f ), obviously, a pointer to the function is created. But a function name can implicitly convert to a function pointer when certain conditions are met. Therefore, the above code can be written as:
typedef void (*fptr_t)(); fptr_t fptr = f;
The second example is executed in this way, but with the help of a temporary variable, hold the pointer to the function instead of the local local variable.
I prefer to use the address when creating function pointers, the meaning is much clearer.
Related Note. A function call operator automatically searches for a pointer to a function, if any. So this is also legal:
typedef void (*fptr_t)(); fptr_t fptr = &f; fptr();
This is very useful with templates because the same syntax works if you have a pointer to a function or functor (an object that implements operator() ).
And none of the labels work with pointers to elements, there you need explicit address and dereference operators.
In C, @Mehrdad explains that all function calls use a function pointer.
source share