Created
November 22, 2018 14:17
-
-
Save RDCH106/a696a95da330ebb2b72d8cd252b2ca0f to your computer and use it in GitHub Desktop.
C++ calback example
This file contains hidden or 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
#include <iostream> | |
#include <string> | |
#include <vector> | |
// Dll | |
typedef bool (*PointerToF)(int number); | |
struct A { | |
PointerToF m_f; | |
void setF(PointerToF f) { | |
m_f = f; | |
} | |
bool call() { | |
return m_f(4); | |
} | |
}; | |
// Ejecutable | |
bool myf(int number) { | |
std::cout << number << std::endl; | |
return number != 0; | |
} | |
int main() | |
{ | |
/* Option A */ | |
A a; | |
a.setF(&myf); | |
a.call(); | |
/* Option B */ | |
// PointerToF una_f = nullptr; | |
// una_f = &myf; | |
// una_f(4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment