- Why should anyone compare function pointers, given that by conception, functions uniqueness is ensured by their different names?
A function pointer can point to different functions at different times in a program.
If you have a variable such as
void (*fptr)(int);
it can point to any function that accepts an int
as input and returns void
.
Let's say you have:
void function1(int)
{
}
void function2(int)
{
}
You can use:
fptr = function1;
foo(fptr);
or:
fptr = function2;
foo(fptr);
You might want to do different things in foo
depending on whether fptr
points to one function or another. Hence, the need for:
if ( fptr == function1 )
{
// Do stuff 1
}
else
{
// Do stuff 2
}
- Does the compiler see function pointers as special pointers? I mean does it see them like, let's say, pointers to void * or does it hold richer information (like return type, number of arguments and arguments types?)
Yes, function pointers are special pointers, different from pointers that point to objects.
The type of a function pointer has all that information at compile time. Hence, give a function pointer, the compiler will have all that information - the return type, the number of arguments and their types.