Advertisement
stgatilov

Pankaj Goyal question (to be removed)

Sep 25th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct BaseHandler {};    //Blank class
  4. typedef void (BaseHandler::*InstantFunc)();
  5.  
  6. struct MyDrivedClass : public BaseHandler {
  7.     char *m_someMember;
  8.     void DoJob() {
  9.         printf("%s\n", m_someMember);
  10.     }
  11.     virtual void qq() {
  12.         printf("!");
  13.     }
  14. };
  15.  
  16. class ForCallback {
  17. public:
  18.     ForCallback(BaseHandler* handler, InstantFunc callback)
  19.     : m_handler(handler), m_callback(callback) {}
  20.     void Handle() {
  21.         (m_handler->*(m_callback))();
  22.     }
  23.  
  24. protected:
  25.     BaseHandler* m_handler;
  26.     InstantFunc  m_callback;
  27. };
  28.  
  29. int main() {
  30.     MyDrivedClass *drived = new MyDrivedClass();
  31.     drived->m_someMember = "ololo";
  32.     drived->qq();
  33.     ForCallback *cBack = new ForCallback(drived, static_cast<InstantFunc>(&MyDrivedClass::DoJob));
  34.     cBack->Handle();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement