Embed presentation
Download as PDF, PPTX

![18/43 Why is C++ lookup simpler?
• Smalltalk has no static type system
– Code p message:pars could refer to any object
– Need to find method using pointer from object
– Different classes will put methods at different place in method
dictionary
• C++ type gives compiler some superclass
– Offset of data, fctn ptr same in subclass and superclass
– Offset of data and function ptr known at compile time
– Code p->move(x) compiles to equivalent of
(*(p->vptr[1]))(p,x) if move is first function in vtable
data passed to member function; see next slides](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-2-2048.jpg)
![19/43
Point p = new Pt(3);
p->move(2); // (*(p->vptr[0]))(p,2)
Looking up methods
3
5
blue
Point object
ColorPoint object
x
vptr
x
vptr
c
Point vtable
ColorPoint vtable
Code for move
Code for move
Code for darken](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-3-2048.jpg)
![20/43 Looking up methods
3
5
blue
darken()
Point object
ColorPoint object
x
vptr
x
vptr
c
Point vtable
ColorPoint vtable
Code for move
Code for move
Code for darken
Point cp = new ColorPt(5,blue);
cp->move(2); // (*(cp->vptr[0]))(cp,2)](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-4-2048.jpg)
C++ method lookup is simpler than Smalltalk because C++ uses static typing and inheritance. In C++, the compiler knows the type of an object and can determine the method offset at compile time based on the class hierarchy. For a method call like p->move(x), the compiler generates code equivalent to (*(p->vptr[1]))(p,x), with the object pointer and parameters passed directly. In Smalltalk, without static types, the runtime must search the method dictionary to find the right method.

![18/43 Why is C++ lookup simpler?
• Smalltalk has no static type system
– Code p message:pars could refer to any object
– Need to find method using pointer from object
– Different classes will put methods at different place in method
dictionary
• C++ type gives compiler some superclass
– Offset of data, fctn ptr same in subclass and superclass
– Offset of data and function ptr known at compile time
– Code p->move(x) compiles to equivalent of
(*(p->vptr[1]))(p,x) if move is first function in vtable
data passed to member function; see next slides](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-2-2048.jpg)
![19/43
Point p = new Pt(3);
p->move(2); // (*(p->vptr[0]))(p,2)
Looking up methods
3
5
blue
Point object
ColorPoint object
x
vptr
x
vptr
c
Point vtable
ColorPoint vtable
Code for move
Code for move
Code for darken](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-3-2048.jpg)
![20/43 Looking up methods
3
5
blue
darken()
Point object
ColorPoint object
x
vptr
x
vptr
c
Point vtable
ColorPoint vtable
Code for move
Code for move
Code for darken
Point cp = new ColorPt(5,blue);
cp->move(2); // (*(cp->vptr[0]))(cp,2)](https://image.slidesharecdn.com/lo18-160512122323/75/Lo18-4-2048.jpg)