Function Objects and Lambda Expressions
One common pattern used in programming, particularly when implementing event-based processing, such as asynchronous input and output, is the use of the callback. A client registers that they want to be notified that an event has occurred (For example: data is available to read, or a data transmission is complete). This pattern is known as Observer pattern or Subscriber Publisher pattern. C++ supports a variety of techniques to provide the callback mechanism.
Function Pointers
The first mechanism is the use of the function pointers. This is a legacy feature inherited from the C language. The following program shows an example of a function pointer:
#include <iostream>
using FnPtr = void (*)(void);
void function1()
{
std::cout << "function1 called\n";
}
int main()
{
std::cout << "\n\n------ Function Pointers ------\n";
FnPtr fn{function1};
...