Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <unordered_map>
- #include <functional>
- class Conn;
- typedef void (Conn::*ProcFn)();
- class Conn
- {
- private:
- void proc_ping();
- static const std::unordered_map<std::string, ProcFn> handlers;
- public:
- void simulate();
- };
- const std::unordered_map<std::string, ProcFn> Conn::handlers = {
- std::pair("ping", &Conn::proc_ping)
- };
- void Conn::proc_ping()
- {
- std::cout << "ping!" << std::endl;
- }
- void Conn::simulate()
- {
- auto pfn = handlers.at("ping");
- std::invoke(pfn, this);
- }
- int main()
- {
- Conn c;
- c.simulate();
- }
- #include <iostream>
- #include <string>
- #include <unordered_map>
- #include <functional>
- class ConnBase;
- typedef void (ConnBase::*ProcFn)();
- class ConnBase
- {
- protected:
- void proc_ping();
- };
- void ConnBase::proc_ping()
- {
- std::cout << "ping!" << std::endl;
- }
- class ConnMgmt : public ConnBase
- {
- protected:
- static const std::unordered_map<std::string, ProcFn> handlers;
- void proc_create_user();
- public:
- void simulate();
- };
- void ConnMgmt::proc_create_user()
- {
- std::cout << "create user!" << std::endl;
- }
- const std::unordered_map<std::string, ProcFn> ConnMgmt::handlers = {
- std::pair("ping", &ConnBase::proc_ping),
- std::pair("create_user", &ConnMgmt::proc_create_user)
- };
- void ConnMgmt::simulate()
- {
- auto pfn = handlers.at("ping");
- std::invoke(pfn, this);
- pfn = handlers.at("create_user");
- std::invoke(pfn, this);
- }
- int main()
- {
- ConnMgmt c;
- c.simulate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement