the difference between the c and c++ ways |
That's a rather large topic.
The C way is the way of the function pointer, which is, well, a pointer (internally, an integer or a few integers, on some architectures). A function pointer can be copied and it can be invoked, by applying the function call operator (the pair of round parentheses with arguments) to it on the right.
The C++ way is the way of the function object, which is also something that can be copied and invoked by applying the function call operator, but, unlike the function pointer, it may hold
state: that is, invoking it not only executes the code but also changes the object's member variables, so that the next time the same function object is invoked, it may do something different.
Examples of function objects are:
user-defined objects of any type with operator() defined
function pointers (C-style function pointer can be used whenever C++ expects a function object)
references to functions (wrapped with std::ref() to be copyable)
bind expressions (that's what std::bind() returns)
closure objects (that's what lambda-expression returns)
the objects returned by std::mem_fn (wrapped member functions)
the objects returned by std::plus, std::less, and many other standard library function objects
std::function objects that wrap any of the above