Created
September 28, 2008 16:55
-
-
Save neel/13478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class matter : public thing{ | |
public: | |
matter(); | |
void four(){ | |
std::cout<<"Four from matter"<<endl; | |
} | |
~matter(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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