Is there a way to have functions as arguments? If I have a function that takes a double and returns a double, is there a way I can make another function that takes the first function as an argument?
Is there a way to have functions as arguments? If I have a function that takes a double and returns a double, is there a way I can make another function that takes the first function as an argument?
like that?Code:int foo(); int foofoo(int var); int main() { int a = foofoo(foo()); return 0; }
Yes, for example:
So func in bar is a function pointer, in this case a function pointer to foo. You could also use function objects instead, which can be more powerful.Code:#include <iostream> double foo(double x); void bar(double (*func)(double), double x); int main() { bar(foo, 1.5); } double foo(double x) { return x + x; } void bar(double (*func)(double), double x) { std::cout << func(x) << std::endl; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)