Skip to content

Instantly share code, notes, and snippets.

@zwpp
Created August 13, 2016 06:21
Show Gist options
  • Save zwpp/e1b7a285af3b13988e920289e1649c0b to your computer and use it in GitHub Desktop.
Save zwpp/e1b7a285af3b13988e920289e1649c0b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <algorithm>
//#include <array>
//#include <vector>
class Nyan {
public:
std::function<void()> fs[2];
void hoge();
void fuga();
void caller();
Nyan();
private:
unsigned stat;
};
Nyan::Nyan() :stat(0) {
auto behaviors = {&Nyan::hoge, &Nyan::fuga};
std::transform(std::begin(behaviors),
std::end(behaviors),
fs,
[this](auto f){ return std::bind(f, this); }
);
};
void Nyan::hoge() {
std::cout << "hoge" << std::endl;
stat = 1;
}
void Nyan::fuga() {
std::cout << "fuga" << std::endl;
stat = 0;
}
void Nyan::caller() {
fs[stat]();
}
int main() {
Nyan n;
n.caller();
n.caller();
n.caller();
n.caller();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment