Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. class B { // Base class
  2. protected:
  3. void (B::*fptest)(); // Function pointer to member function
  4. public:
  5. void mtest() // Class specific mebmer function
  6. { cout << "Base testn"; }
  7. void itest() // Polymorphic function
  8. { (this->*fptest)(); } // implemented by calling the poitner to member function
  9. B() : fptest(&B::mtest) { } // Ctor must initialize the function pointer
  10. virtual ~B() {}
  11. };
  12.  
  13. class D : public B { // Derived class
  14. public:
  15. void mtest() // Class specific mebmer function
  16. { cout << "Derived testn"; }
  17. D() // Ctor
  18. { fptest = reinterpret_cast<void(B::*)()>(&D::mtest); } // not sure it's this safe in case of multiple inheritance !!
  19. };
  20.  
  21. B b;
  22. D d;
  23. B *pb = &b, *pd = &d;
  24. pb->itest();
  25. pd->itest();
  26.  
  27. ; 41 : pd->itest(); // cod for the call for a derived object
  28. mov ecx, DWORD PTR _pd$[ebp] ; load the oject address
  29. call ?itest@B@@QAEXXZ ; call B::itest
  30. ; 16 : void itest() { (this->*fptest)(); }
  31. push ebp
  32. mov ebp, esp
  33. sub esp, 68 ; 00000044H
  34. push ebx
  35. push esi
  36. push edi
  37. mov DWORD PTR _this$[ebp], ecx ; use address of object as parameter
  38. mov eax, DWORD PTR _this$[ebp] ; load the function pointer
  39. mov ecx, DWORD PTR _this$[ebp] ; " "
  40. mov edx, DWORD PTR [eax+4] ; call the function pointer
  41. call edx
  42. pop edi
  43. pop esi
  44. pop ebx
  45. mov esp, ebp
  46. pop ebp
  47. ret 0
  48.  
  49. 39 : pd->test();
  50. mov eax, DWORD PTR _pd$[ebp]
  51. mov edx, DWORD PTR [eax]
  52. mov ecx, DWORD PTR _pd$[ebp]
  53. mov eax, DWORD PTR [edx]
  54. call eax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement