Created
December 9, 2019 18:36
-
-
Save zigo101/28ecd6ad2425898f2cbf68b7302bd95f to your computer and use it in GitHub Desktop.
use C++ member functions as callbacks
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
#include <functional> | |
#include <iostream> | |
using namespace std; | |
class C | |
{ | |
public: | |
int foo; | |
void m() | |
{ | |
cout << foo << endl; | |
} | |
}; | |
void g(std::function<void()> f) | |
{ | |
f(); | |
} | |
int main() | |
{ | |
C c; | |
c.foo = 123; | |
g(std::bind(&C::m, &c)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment