Created
January 11, 2018 12:14
-
-
Save xaedes/e260d79fd16cb7a78213b512f4c71d5a to your computer and use it in GitHub Desktop.
Callback.cpp
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
// Example program | |
#include <iostream> | |
#include <string> | |
#include <functional> | |
struct PipedBuffer | |
{ | |
int bufSize; | |
}; | |
typedef std::function<void(PipedBuffer*)> CallbackFunction; | |
void registerSubscriber(CallbackFunction f) | |
{ | |
PipedBuffer buf{10}; | |
f(&buf); | |
} | |
class Foo{ | |
public: | |
Foo() { | |
registerSubscriber( std::bind(&Foo::fooCallback, this, std::placeholders::_1) ); | |
} | |
void fooCallback(PipedBuffer* buf) { | |
std::cout << "Foo::fooCallback" << std::endl; | |
std::cout << buf->bufSize << std::endl; | |
} | |
}; | |
void barCallback(PipedBuffer* buf) { | |
std::cout << "barCallback" << std::endl; | |
std::cout << buf->bufSize << std::endl; | |
} | |
int main() | |
{ | |
Foo foo; | |
registerSubscriber( std::bind(&barCallback, std::placeholders::_1) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment