Khi class có ít nhất 1 virtual function, compiler tạo một vtable — mảng các con trỏ hàm cho class đó.
Mỗi object có thêm một hidden pointer (vptr) trỏ tới vtable của class thực sự.
Shape vtable: [ draw → Shape::draw, area → 0 (pure) ]
Circle vtable: [ draw → Shape::draw, area → Circle::area ]Khi gọi s->area() (s là Shape* trỏ Circle): đọc vptr → tra vtable → gọi Circle::area. Toàn bộ quá trình trong 1 indirect call.
Chi phí: thêm 1 pointer (~8 bytes) mỗi object + 1 indirect function call. Trong phần lớn ứng dụng, chi phí này không đáng kể.
Lý do phải khai báo virtual destructor: nếu không, khi delete shape_ptr compiler chỉ gọi Shape::~Shape bỏ qua Circle::~Circle → undefined behavior (biểu hiện thường là memory leak hoặc resource không được giải phóng).
When a class has at least one virtual function, the compiler creates a vtable — an array of function pointers for that class.
Each object gets a hidden vptr pointing to its actual class's vtable.
Shape vtable: [ draw → Shape::draw, area → 0 (pure) ]
Circle vtable: [ draw → Shape::draw, area → Circle::area ]Calling s->area() (s is Shape* pointing to Circle): read vptr → look up vtable → call Circle::area. One indirect call total.
Cost: one extra pointer (~8 bytes) per object + one indirect function call instead of a direct call. Negligible in most applications.
Why declare a virtual destructor: without it, delete shape_ptr only calls Shape::~Shape and skips Circle::~Circle → undefined behavior (commonly manifests as memory leak or unreleased resources).