We had a homework assignment which is over now and the professor gave us the solutions. The problem was just to come up with a situation where we could use function pointers and the code it.
His example is this:
#include <iostream>
using namespace std;
int dvalue(int x)
{
return x*x;
}
int tvalue(int x)
{
return x*x*x;
}
void printValue(int (*fptr)(int), int x)
{
cout << fptr(x) << endl;
}
int main()
{
int (*pfnc)(int);
int value = 10;
pfnc = dvalue;
printValue(pfnc, 10);
}
My questions are, is that the same thing as doing this:
int main()
{
cout << devalue(10) << endl;
}
And why do we use them? Do they use less memory? Is it just so we can call more than one function to a parameter or another function to make it simpler? It seems that they are more complicated, at least for a beginning programmer.