3.18. C++
push OFFSET ??_C@_0DO@HNCNIHNN@this?5is?5solid_box?4?5width?$DN?$CFd?0?5hei@
call _printf
add esp, 20
ret 0
?dump@solid_box@@QAEXXZ ENDP ; solid_box::dump
So, the memory layout for all three classes is:
boxclass:
offset description
+0x0 width
+0x4 height
+0x8 depth
solid_objectclass:
offset description
+0x0 density
It can be said that thesolid_boxclass memory layout isunited:
solid_boxclass:
offset description
+0x0 width
+0x4 height
+0x8 depth
+0xC density
The code of thebox::get_volume()andsolid_object::get_density()methods is trivial:
Listing 3.95: Optimizing MSVC 2008 /Ob0
?get_volume@box@@QAEHXZ PROC ; box::get_volume, COMDAT
; _this$ = ecx
mov eax, DWORD PTR [ecx+8]
imul eax, DWORD PTR [ecx+4]
imul eax, DWORD PTR [ecx]
ret 0
?get_volume@box@@QAEHXZ ENDP ; box::get_volume
Listing 3.96: Optimizing MSVC 2008 /Ob0
?get_density@solid_object@@QAEHXZ PROC ; solid_object::get_density, COMDAT
; _this$ = ecx
mov eax, DWORD PTR [ecx]
ret 0
?get_density@solid_object@@QAEHXZ ENDP ; solid_object::get_density
But the code of thesolid_box::get_weight()method is much more interesting:
Listing 3.97: Optimizing MSVC 2008 /Ob0
?get_weight@solid_box@@QAEHXZ PROC ; solid_box::get_weight, COMDAT
; _this$ = ecx
push esi
mov esi, ecx
push edi
lea ecx, DWORD PTR [esi+12]
call ?get_density@solid_object@@QAEHXZ ; solid_object::get_density
mov ecx, esi
mov edi, eax
call ?get_volume@box@@QAEHXZ ; box::get_volume
imul eax, edi
pop edi
pop esi
ret 0
?get_weight@solid_box@@QAEHXZ ENDP ; solid_box::get_weight
get_weight()justcallstwomethods,butforget_volume()itjustpassespointertothis,andforget_density()
it passes a pointer tothisincremented by 12 (or0xC) bytes, and there, in thesolid_boxclass memory
layout, the fields of thesolid_objectclass start.