Is there any way to pass a function as a parameter in C++, like the way that functions can be passed as parameters in C? I know that it’s possible to pass a function as a parameter in C using function pointers, and I want to know whether the same is possible in C++.
Share
You can do it like in C. But you can also do it the C++ way (C++11, to be exact):
You can pass a normal function to foo()
but you can also pass a lambda expression. For example:
You can also pass a function object (an object that overloads the
()operator.) In general, you can pass anything that can be called with the()operator.If you instead use the C way, then the only thing you can pass are normal function pointers.