CSE687 - Object Oriented Design

Language Note #4
Virtual Function Pointer Tables

Revised: 4/28/2011
Home Courses Code Handouts CoreTech Books Articles Math Research Masters Projects SWDev WebDev News OtherLinks SiteDesign Graduates AllPages Office Hours Notices

CSE681-SWMAA CSE686-IP CSE687-OOD CSE775-DO CSE776-DP CSE778-AWP CSE784-SWS

Cncp #01 Cncp #02 Cncp #03 Cncp #04 Cncp #05 Cncp #06 Cncp #07 Cncp #08 Cncp #09 Cncp #10 Cncp #11
Note #01 Note #02 Note #03 Note #04 Note #05 Note #06 Note #07 Note #08
Lang #01 Lang #02 Lang #03 Lang #04

Syllabus SG - Design SG - Templates SG - Class Relationships

Language Note #4:

Pulic inheritance supports a powerful form of substitution. Wherever your code holds a pointer or reference to a base class, it will compile and operate correctly if you substitue a pointer or reference to an instance of a class derived from that base. We call this behavior polymorphism.

This enables the design of sets of specialized classes all of which share a common language and may share some common code and allocated resources. A graphics editor, for example, might use a graphicsObject base class from which are derived things like lines, circles, rectangles, etc.. The editor can then hold a draw list of graphicsObjects, in which we insert whatever mix of specialized objects we need to build some diagram. If translate is part of the base class public interface - its language - then the editor can translate the entire diagram simply by iterating through its draw list, calling translate on each of its elements, irrespective of whether that element is a line or rectangle or circle.

This specialized substitution magic is implemented using Virtual Function Pointer Tables (VFPTs) as sketched in the diagram, below.

image file not found

Virtual Function Pointer Table


Conclusions for: Virtual Function Pointer Tables

Polymorphism in C++ is based on the use of Virtual Function Pointer Tables (VFPTs)
  1. Each class that defines virtual functions is endowed with a VFPT.
  2. Table entries contain pointers to the virtual functions for that class.
  3. Classes publically derived from some base have pointers to all the base's virtual functions in the VFPT by default.
  4. For each virtual function the derived class overrides, by providing a definition, the VTBL points to the overriding code, instead of the base version of that function.
  5. The C++ language supports definition of a base pointer initialized with the address of an instance of any of its derived classes. That pointer can be used to invoke any function declared in the base interface, whether virtual or non-virtual.
  6. If the function called is an overridden virtual function, then the derived's definition is invoked. otherwise the base definition is invoked.
  7. This assumes that the derived class has not defined a non-virtual function also provided by the base, which you should never do. Yes, never, even though code that does this will compile, because it then runs with incorrect and possibly disasterous semantics.