| C++ Program Design, 3/e James P. Cohoon,
University of Virginia Jack W. Davidson,
University of Virginia
Templates and polymorphism
Chapter 14 OverviewPolymorphism is a language mechanism that permits the same code expression
to invoke different functions depending on the type of objects using the code.
Name reuse in function overloading is a primitive form of polymorphism. In
this chapter we explore the two major C++ mechanisms for providing
polymorphic capabilities. The first method is via class and function templates.
A template, when invoked with particular types and values, can generate a new
function or class. All functions or classes generated from a given template have
the same name. The second method is via virtual member functions. In an
expression involving a virtual function invocation, the decision on which
function to use is delayed until run time. The decision is then based on the type
of object being referenced in the invocation. This type of polymorphism is
sometimes called pure polymorphism. |
|