// pointer to functions
#include <iostream>
usingnamespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
Here in the main function, "m = operation (7, 5, addition);" shows that addition function is passed in as a parameter. Next line "n = operation (20, m, minus);" shows that a function pointer "minus" which points to subtraction function is passed in as a parameter. On the first time, how does it just pass in a function as a parameter when it's expecting a function pointer? If so, what would ever be the point of making another function pointer to point to a function then passing in this pointer as a parameter than just passing in a function right away?
Personally I have never used this. I would just use the function itself. The only pros I can see with this is that it may look cleaner in main?? And for the user they might actually prefer to write operation everytime rather then different function names, but they still have to pass in the function name.
I think this was more of a "This is Possible" sort of thing. I havnt seen this done very often myself and I dont even see where it is beneficial.
On the first time, how does it just pass in a function as a parameter when it's expecting a function pointer?
If I understand your question right... this is C shorthand. I agree it's not very consistent.
If you have a global function and you don't put the parenthesis after it, you're left with a function pointer. IE:
1 2
int (*minus)(int,int) = subtraction; // This 'subtraction' is a function pointer -- no parenthesis
subtraction(2,1); // This 'subtraction' is a function call -- because there's parenthesis
Another (more clear, IMO) way to show it's a function pointer is to use the &:
int (*minus)(int,int) = &subtraction; // same as above
------------------
what would ever be the point of making another function pointer to point to a function then passing in this pointer as a parameter than just passing in a function right away?
I think the example was just showing how you could create a function pointer variable and assign it.
It was also showing how to have function pointers as parameters.
I don't think the code was meant to be practical. It's just showing off the syntax.
Personally I have never used this. I would just use the function itself. The only pros I can see with this is that it may look cleaner in main?? And for the user they might actually prefer to write operation everytime rather then different function names, but they still have to pass in the function name.
I think this was more of a "This is Possible" sort of thing. I havnt seen this done very often myself and I dont even see where it is beneficial.
Don't give opinions about topics you don't understand.
This is a type of dynamic binding. The behavior of a function can be changed by having it call different functions. The simplest example is qsort() in the C standard library.
On the first time, how does it just pass in a function as a parameter when it's expecting a function pointer?
The name of a function is a function pointer itself.
what would ever be the point of making another function pointer to point to a function then passing in this pointer as a parameter than just passing in a function right away?
It was a demonstration of the syntax of function pointer declaration and assignment, I guess.
Something the example don't mention is that there's a bit of syntactical sugar when it comes to calling functions through pointers. Line 14 can be rewritten as g = functocall(x,y);
Personally, I think it's much nicer, if a bit ambiguous.
Personally, I think it's much nicer, if a bit ambiguous.
Heh, that's more C shorthand. I prefer the other method.
It's worth mentioning that those shortcuts can only be taken with global function pointers and not with member function pointers. With member functions you need the appropriate & and *.
Don't give opinions about topics you don't understand.
This is a type of dynamic binding. The behavior of a function can be changed by having it call different functions. The simplest example is qsort() in the C standard library.
If I hadnt I wouldnt have been scolded on the issue. At least now I know a little more.
Especially this
The behavior of a function can be changed by having it call different functions
One more question about the line: g = (*functocall)(x,y);
Why do you need a * there? and can you explain a bit more about what you mean by
It's worth mentioning that those shortcuts can only be taken with global function pointers and not with member function pointers. With member functions you need the appropriate & and *.
As helios said g = (*functocall)(x,y); is the same as g = functocall(x,y);
The * is just emphasizes dereferencing the function pointer.
and can you explain a bit more about what you mean by [member function pointers]
member function pointers are pointers to classes' member functions. You probably will only use it very rarely (if ever). There is a lot of information about them, Google it.
Member function pointers are mostly broken, in C++. That's fine, however, because they have very limited application. The only time I've used them was to implement an interpreter that would call functions stored in an std::wstring-keyed std::map to respond to user commands.
Now time for me to scold both R0mai and helios for close-minded statements such as
You probably will only use it very rarely (if ever)
[quote]
and
[quote]
Member function pointers are mostly broken, in C++. That's fine, however, because they have very limited application.
Au contraire, mon frere. I use them ALL the time. The fact that there is a wealth of information about them and the fact that numerous boost libraries exist that either use them or make them easier to use contradict both of the above statements. Witness boost::function and boost::bind, for example. boost::lambda also provides its own version of bind.
Function pointers are commonly used to implement callback mechanisms, which is an equally
valid concept in C++ as it is in C.
And what exactly prompts the statement that member function pointers are "mostly broken,
in C++"?????