Last active
January 27, 2018 03:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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