1) I'm glad you liked it. But regarding the function operation it accepts three parameters. 1 and 2 are just integers, obviously, while the third is the function pointer itself. It is basically a mini prototype. It says that when calling the function FuncToCall, you need to pass it two parameters, int a and int b.
If you think about it, it's just a new name for another function. But you have to look at what function pointer was passed, so you somewhat have to work backwards.
operation gets called with num1, num2, and mathFunc.
mathFunc get's assigned the function either addition, subtraction, etc.
Looking at the declaration of mathFunc, you see that it's "prototype" if you will says that it accepts two int values, so it is allowed to point at any function that also accepts and two int values. Again, this isn't very effective considering the fact that it's not a very in depth system and I haven't played with the C function pointers at all before this.
But the thing to remember is that a function pointer can be very useful when you want to run a function when you call another function, but have no idea what that function is going to be. Using C function pointers, you're limited to what you can call. However, using C++ functional, you have the option of not only directly calling a function, but you can pass a bound function or a lambda function, both of which require some thinking to accomplish.
I can't really explain it better, but if you're really trying to learn C function pointers, I suggest creating a new thread in the General Programming forum so you can get some better explanations on it.
2) They started out as prototypes, but I got lazy and just decided to define them there. I simply forgot to change my comment.
As for this line:
return ((b == 0) ? b : a / b);
it's a simple return statement, but it returns the value of a ternary condition. A ternary condition is a basic inline if/else statement. The syntax is:
1 2 3 4 5 6
|
(condition) ? trueExpression : falseExpression;
// Which can be wrote like
if (condition)
trueExpression;
else
falseExpression;
|
Putting it back into context, it's the same as writing this:
1 2 3 4
|
if (b == 0)
return 0;
else
return a / b;
|
Sometimes I get carried away with using them, but I figured it was ok just to slide that in to prevent dividing by 0.
3) I figured it was over your head, but it is a good overall reference. It goes into depth over three good C++ features, std::function, std::bind, and lambda, which is what [] means. Each one of those features takes a fairly long time to master, but has it's place in the language. Most of it was an attempt to fill in gaps that the standard was missing, or things that were never converted to C++ from C.
As for function pointers vs functional, I highly suggest functional. It's much easier to understand since it has it's own class type and you can easily pass anything you want to it, especially with the introduction of lambdas.
On a side note, don't get worked up over the ugliness of lambda expressions, they're quite ugly, but albeit effective.