David Lindauer posted:
JKop wrote:
David Lindauer posted:
> There are two types of function pointers.
>
> 1) pointer to a non-member function
>
> int nn(int b) ; // define a function
That's "declare a function".
> int (*aa)(int b) ; // define a pointer
> aa = nn ; // assign the function to the pointer int
> bb = (*aa)(5) ; // call the function
Yes; but 99% of the time, that's written as:
int bb = aa(5);
maybe to you... I prefer the former because it makes it very plain you
are calling through a variable. Such things make a huge difference when
you hand off a program to someone else... they can immediately see what
is going on.
David
But at the same time, the asterisk is redundant.
Take a variable/object for example. If it's type is "int*", then when you
stick an asterisk in front of it, you've got an expression of type "int".
Like so:
int* p = ...;
*p;
Now take a pointer to a function. If it's type is "int (*)(double)", then
when you stick an asterisk in front of it, you've got an expression of type
"int (*)(double)". Nothing has changed - the asterisk is redundant.
Anyway; as regards making it clear that it's being called "through a
pointer", I don't see the relevance - it doesn't have to be worked with any
differently. Take pointer variables for example, I usually prefix "p_" to
their name to clearly indicate that it's a pointer, because pointers are
work with differently to normal variables/objects. But this isn't so for
pointers to functions, they work exactly the same - there's no need to say
"Look at me, I'm really a pointer, keep it in mind".
Also, I'd prefer a reference to a function wherever possible.
-JKop