Reverse Engineering for Beginners

(avery) #1

CHAPTER 51. C++ CHAPTER 51. C++


printf ("this is box. color=%d, width=%d, height=%d, depth=%d\n", color, width,⤦
Çheight, depth);
};
};


class sphere : public object
{
private:
int radius;
public:
sphere(int color, int radius)
{
this->color=color;
this->radius=radius;
};
void dump()
{
printf ("this is sphere. color=%d, radius=%d\n", color, radius);
};
};


int main()
{
box b(1, 10, 20, 30);
sphere s(2, 40);


object *o1=&b;
object *o2=&s;

o1->dump();
o2->dump();
return 0;
};


Classobjecthas a virtual methoddump()that is being replaced in the inheritingboxandsphereclasses.


If we are in an environment where it is not known the type of an object, as in themain()function in example, where the
virtual methoddump()is called, the information about its type must be stored somewhere, to be able to call the relevant
virtual method.


Let’s compile it in MSVC 2008 with the/Oxand/Ob0options and see the code ofmain():


_s$ = -32 ; size = 12
_b$ = -20 ; size = 20
_main PROC
sub esp, 32
push 30
push 20
push 10
push 1
lea ecx, DWORD PTR _b$[esp+48]
call ??0box@@QAE@HHHH@Z ; box::box
push 40
push 2
lea ecx, DWORD PTR _s$[esp+40]
call ??0sphere@@QAE@HH@Z ; sphere::sphere
mov eax, DWORD PTR _b$[esp+32]
mov edx, DWORD PTR [eax]
lea ecx, DWORD PTR _b$[esp+32]
call edx
mov eax, DWORD PTR _s$[esp+32]
mov edx, DWORD PTR [eax]
lea ecx, DWORD PTR _s$[esp+32]
call edx
xor eax, eax
add esp, 32
ret 0
_main ENDP

Free download pdf