Virtual Member Functions and Dynamic Binding
Let’s revisit the process of invoking a method with a reference or pointer. Consider the following code:
BrassPlus ophelia; // derived-class objectBrass * bp; // base-class pointerbp = &ophelia; // Brass pointer to BrassPlus objectbp->ViewAcct(); // which version?
As discussed before, if
ViewAcct() is not declared as virtual in the base class,
bp->ViewAcct() goes by the pointer type (
Brass *) and invokes
Brass::ViewAcct(). The pointer type is known at compile time, so the compiler can bind
Brass::ViewAcct() at compile time. In short, the compiler uses static binding for nonvirtual methods.
ViewAcct() is declared as virtual in the base class,