class C
{
doubleoperator() ( double x ); // operator () overloaded
};
C functor;
functor(123); // you can use functor as a function thanks to the () operator
function pointers are a different thing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
double function1 ( double x, double y )
{
return x+y;
}
double function2 ( double x, double y )
{
return x-y;
}
double (*function_pointer) ( double, double ); // declaring function_pointer
function_pointer = function1; //assign function_pointer to function1
function_pointer ( 5, 3 ); // returns 8, the same as function1
function_pointer = function2; //assign function_pointer to function2
function_pointer ( 5, 3 ); // returns 2, the same as function2
By default a set sorts its elements in ascending order. The above code compiled and displays numbers in descending order, which is fine. I thought that the code would not compile due to the last parameter in for_each loop. I thought that the last parameter should be passing a parameter to the calling function. Is that correct?