apply()
code existed and was compiled long before class Circle was createdapply()
code in this case must call Circle::draw()
We can solve any problem by introducing an extra level of indirection
All problems in computer science can be solved by another level of indirection, except for the problem of too many layers of indirection
method[0]
for the first virtual method, method[1]
for the second virtual method, etc.Shape* p = new Shape(); p->draw();
Shape* p
points to the new Shape objectdraw()
is the first entry in the vtableShape::draw()
Shape* p = new Circle(); p->draw();
Shape* p
points to the new Circle objectdraw()
is the first entry in the vtableCircle::draw()
p->draw()
class Derived
on class Base
because changes to the class Base
vtable (e.g., add or remove virtual methods) changes the layout of the class Derived
vtableapply()
) can only call class Base methods