c:c_callback_functions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c:c_callback_functions [2021/04/28 08:27] – created peter | c:c_callback_functions [2021/04/28 08:34] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== C - C++ - Callback functions ====== | + | ====== C - C++ Callback functions ====== |
+ | ===== callback_function.hpp ===== | ||
- | [[C:C++ Callback functions|C++ Callback functions]] | + | <code cpp> |
+ | #pragma once | ||
+ | |||
+ | #include < | ||
+ | |||
+ | // Define a callback function based on a class method and call it with argument " | ||
+ | void use_callback_function(std::string output); | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== callback_function.cpp ===== | ||
+ | |||
+ | |||
+ | <code cpp> | ||
+ | #include " | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | class MyClass | ||
+ | { | ||
+ | public: | ||
+ | static void mystaticfunction(std:: | ||
+ | { | ||
+ | std::cout << str << std:: | ||
+ | } | ||
+ | |||
+ | |||
+ | void mymemberfunction(std:: | ||
+ | { | ||
+ | mystaticfunction(str); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | |||
+ | void test(std:: | ||
+ | { | ||
+ | if (callBack) | ||
+ | { | ||
+ | callBack(str); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void use_callback_function(std:: | ||
+ | { | ||
+ | // Sample 1. | ||
+ | test(output, | ||
+ | |||
+ | |||
+ | // Sample 2. | ||
+ | MyClass myClass; | ||
+ | auto callBack = [& | ||
+ | { | ||
+ | myClass.mymemberfunction(str); | ||
+ | }; | ||
+ | |||
+ | test(output, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | https:// |
c/c_callback_functions.1619598478.txt.gz · Last modified: 2021/04/28 08:27 by peter