Skip to content

Instantly share code, notes, and snippets.

@neel
Created September 28, 2008 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neel/13478 to your computer and use it in GitHub Desktop.
Save neel/13478 to your computer and use it in GitHub Desktop.
int main(int argc, char *argv[]){
thing t;
t.addCallback("one", &thing::one);
t.addCallback("two", &thing::two);
t.addCallback("three", &thing::three);
t.call("one");
t.call("two");
t.call("three");
matter m;
m.addCallback("one", &thing::one);
//m.addCallback("one", &thing::four);//Doesn't Compile error: 'four' is not a member of 'thing'
//m.addCallback("one", &matter::four);
/*
The above one also doesn't compile it says
error: no matching function for call to 'matter::addCallback(const char [4], void (matter::*)())'
note: candidates are: void thing::addCallback(const std::string&, p;, void (thing::*)())
*/
m.call("one");
}
class matter : public thing{
public:
matter();
void four(){
std::cout<<"Four from matter"<<endl;
}
~matter();
};
class thing{
private:
typedef void (thing::*_fptr)();
std::map<std::string, _fptr> mfp;
public:
thing(){
}
~thing();
void one(){
std::cout<<"One"<<endl;
}
void two(){
std::cout<<"two"<<endl;
}
void three(){
std::cout<<"Three"<<endl;
}
void call(std::string func){
(this->*(this->mfp[func]))();
}
void addCallback(const std::string& k, _fptr fn){
mfp[k] = fn;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment