virtual dispatch

virtual 호출은 정적 타입이 아니라 실제 객체의 override로 간다

호출 코드는 Base 인터페이스만 보지만, virtual 함수는 런타임 객체의 vtable을 따라 실제 구현을 찾는다.

호출 상황정적 타입실제 객체호출 결과
Shape s; s.draw()ShapeShapeShape::draw
Shape* p = &circle; p->draw()Shape*CircleCircle::draw
Shape& r = rect; r.draw()Shape&RectangleRectangle::draw
delete Shape* pShape*Derivedvirtual 소멸자가 있어야 Derived 소멸자까지 호출
virtual 누락Shape*Circle정적 타입 기준 Shape::draw로 묶일 위험