UMBC CMSC 202 CSEE | 202 | current 202

Binding

Binding

The determination of which method in the class hierarchy is to be invoked for a particular object.

Static (Early) Binding occurs at compile time

When the compiler can determine which method in the class hierarchy to use for a particular object.

Time time1, *tPtr = &time1; ZonedTime zTime1, *ztPtr = &zTime1; time1.SetTime(12, 30, 00); zTime1.SetZonedTime(13, 45, 30); time1.PrintTime( ); // Time's PrintTime ( ) method zTime1.PrintTime( ); // ZonedTime's PrintTime( ) method tPtr->PrintTime( ); // Time's PrintTime( ) ztPtr->PrintTime( ); // ZonedTime's PrintTime( )

Dynamic (Late) Binding occurs at run time

When the compiler cannot determine the binding of an object to any method.

To indicate that a method is to be bound dynamically, the class must use the reserved word virtual in the method's prototype. When a method is defined as virtual, all overriding methods from that point on down the hierarchy are virtual, even if not explicitly defined to be so. For clarity, explicitly use the virtual reserved word.


Last Modified: Monday, 28-Aug-2006 10:16:04 EDT