Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class B { // Base class
- protected:
- void (B::*fptest)(); // Function pointer to member function
- public:
- void mtest() // Class specific mebmer function
- { cout << "Base testn"; }
- void itest() // Polymorphic function
- { (this->*fptest)(); } // implemented by calling the poitner to member function
- B() : fptest(&B::mtest) { } // Ctor must initialize the function pointer
- virtual ~B() {}
- };
- class D : public B { // Derived class
- public:
- void mtest() // Class specific mebmer function
- { cout << "Derived testn"; }
- D() // Ctor
- { fptest = reinterpret_cast<void(B::*)()>(&D::mtest); } // not sure it's this safe in case of multiple inheritance !!
- };
- B b;
- D d;
- B *pb = &b, *pd = &d;
- pb->itest();
- pd->itest();
- ; 41 : pd->itest(); // cod for the call for a derived object
- mov ecx, DWORD PTR _pd$[ebp] ; load the oject address
- call ?itest@B@@QAEXXZ ; call B::itest
- ; 16 : void itest() { (this->*fptest)(); }
- push ebp
- mov ebp, esp
- sub esp, 68 ; 00000044H
- push ebx
- push esi
- push edi
- mov DWORD PTR _this$[ebp], ecx ; use address of object as parameter
- mov eax, DWORD PTR _this$[ebp] ; load the function pointer
- mov ecx, DWORD PTR _this$[ebp] ; " "
- mov edx, DWORD PTR [eax+4] ; call the function pointer
- call edx
- pop edi
- pop esi
- pop ebx
- mov esp, ebp
- pop ebp
- ret 0
- 39 : pd->test();
- mov eax, DWORD PTR _pd$[ebp]
- mov edx, DWORD PTR [eax]
- mov ecx, DWORD PTR _pd$[ebp]
- mov eax, DWORD PTR [edx]
- call eax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement