Skip to content

Instantly share code, notes, and snippets.

@leifbladt
Created November 27, 2012 12:20
Show Gist options
  • Save leifbladt/4153953 to your computer and use it in GitHub Desktop.
Save leifbladt/4153953 to your computer and use it in GitHub Desktop.
Publish subscribe
#include <iostream>
using namespace std;
class Broker {
public:
Broker() : pos(0) {}
void subscribe(void (*f)()) {
_f[pos] = f;
pos++;
}
void notify() {
for (int i = 0; i < pos; i++) {
(_f[i])();
}
}
private:
void (*_f[10])();
int pos;
};
void m2() {
cout << "m2";
}
int main() {
Broker b;
b.subscribe(&m2);
b.subscribe(&m2);
b.notify();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment