Skip to content

Instantly share code, notes, and snippets.

@aipi
Last active January 27, 2018 03:46
Show Gist options
  • Save aipi/149131639539675a2885ee7b459a70ac to your computer and use it in GitHub Desktop.
Save aipi/149131639539675a2885ee7b459a70ac to your computer and use it in GitHub Desktop.
An callback simple example with C++ to undertanding how it works and its aspects.
/* Callback example with c++ */
#include <iostream>
int add(int a, int b, const char *caller_name)
{
std::cout << __FUNCTION__ << " was called by " << caller_name << "\n";
return a + b;
}
int subtraction(int a, int b, const char *caller_name)
{
std::cout << __FUNCTION__ << " was called by " << caller_name << "\n";
return a - b;
}
int operation(int a, int b, int (*func)(int, int, const char *), const char *caller_name)
{
std::cout << __FUNCTION__ << " was called by " << caller_name << "\n";
int x = (*func)(a, b, __FUNCTION__);
return x;
}
int main()
{
int x, y;
int (*minus)(int, int, const char *) = subtraction;
x = operation(10, 20, add, __FUNCTION__);
y = operation(x, 2, minus, __FUNCTION__);
std::cout << y << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment