public
Last active

  • Download Gist
main.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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");
}
matter.h
C++
1 2 3 4 5 6 7 8 9
class matter : public thing{
public:
matter();
void four(){
std::cout<<"Four from matter"<<endl;
}
~matter();
 
};
thing.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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;
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.