Contents Up << >>

How can C++ achieve dynamic binding yet also static typing?

In the following discussion, "ptr" means either a pointer or a reference.

When you have a ptr, there are two types: the (static) type of the ptr, and the (dynamic) type of the pointed-to object (the object may actually be of a class that is derived from the class of the ptr).

"Static typing" means that the "legality" of the call is checked based on the static type of the ptr: if the type of the ptr can handle the member fn, certainly the pointed-to object can handle it as well.

"Dynamic binding" means that the "code" that is called is based on the dynamic type of the pointed-to object. This is called "dynamic binding," since the actual code being called is determined dynamically (at run time).