Object-Oriented Programming

Dispatch

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Context

  • function free function
  • method member function

Executable Environment

  • C language has:
    • functions (with no overloading)
    • No classes
  • CPU supports:
    • Assembly labels that refer to a memory address
    • Instructions to:
      • callq (or call, call1) call a function located at a memory location
      • retq (or ret, retl) return from a function to the place it is called
    • We will ignore argument passing and return location, which is all handled in the call stack

C++ Function/Methods → Assembly

  • mangling, or name mangling

    method/function names translated into Assembly labels

Mangling Standard

  • There is no standard name mangling for C++
  • Many compilers follow the Titanium C++ ABI (Application Binary Interface)

    “This includes the memory layout for C++ data objects, including both predefined and user-defined data types, as well as internal compiler generated objects such as virtual tables. It also includes function calling interfaces, exception handling interfaces, global naming, and various object code conventions.”

Tools:

  • List symbols: nm overloading
  • Demangle a symbol: c++filt <symbol_name>
  • Generate assembly: g++ -S overloading.cpp

Overloading Example

  • function
  • overloaded function
  • class names
  • method
  • separate compilation
  • embedded assembly

C++ Function/Method Name Rules

  • Case matters
  • Number and type of parameters matters
  • Return type does NOT matter
  • Do not use leading underscores on names (reserved for implementors and standard libraries)

Dispatch

  • dispatch

    selecting which implementation of an operation (method or function) to call

  • Types:
    • static dispatch
    • dynamic dispatch

Static Dispatch

selecting which implementation of an operation (method or function) to call at compile time

  • Which operation (method or function) will be called is determined at compile time
  • Fast as a call can be made
  • Preferred by the compiler for these reasons

C++ Static Dispatch

  • functions
  • static methods
  • non-virtual methods
  • virtual methods not via a pointer or reference

Example

Dynamic Dispatch

selecting which implementation of a polymorphic operation (method or function) to call at run time

  • Requires additional data in the executable
  • C++ only supports single dispatch.

vtable

  • virtual table
  • Created for every class that has virtual methods, or derives from a class with virtual methods
  • First part if for typeinfo and other things
  • Rest is an array of function pointers