Skip to content

Instantly share code, notes, and snippets.

@xaedes
Created January 11, 2018 12:14
Show Gist options
  • Save xaedes/e260d79fd16cb7a78213b512f4c71d5a to your computer and use it in GitHub Desktop.
Save xaedes/e260d79fd16cb7a78213b512f4c71d5a to your computer and use it in GitHub Desktop.
Callback.cpp
// 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