oopshigh priority
How Virtual Dispatch Really Works (vtable indexing)
🔑 A virtual call is vptr + a compile-time-fixed slot index: (*obj->vptr[i])(obj). No name lookup.
1 / 8
example.cpp
1struct Shape {2 virtual double area(); // virtual #0 -> vtable slot [0]3 virtual const char* name(); // virtual #1 -> vtable slot [1]4 int id; // data member5};6struct Circle : Shape {7 double area() override; // overrides slot [0]8 double r; // data member9};10 11Shape* s = new Circle{}; // vptr set to Circle's vtable12double a = s->area(); // (*s->vptr[0])(s)Each virtual reserves a fixed slot index
At compile time the compiler walks Shape's virtual functions in declaration order and reserves a fixed slot: area() -> [0], name() -> [1]. These indices are baked into the binary and never change.
Tap ▶ to play · tap the dots or Next → to stepPlays automatically · Space to play/pause · ← / → to step · controls are above the diagram