Fast dynamic casting

  • Authors:
  • Michael Gibbs;Bjarne Stroustrup

  • Affiliations:
  • Lockheed Martin Aerospace, MZ 6602, P.O. Box 748, Fort Worth, TX 76101, U.S.A.;Department of Computer Science, TAMU 3112, Texas A&M University, College Station, TX 77843, U.S.A.

  • Venue:
  • Software—Practice & Experience
  • Year:
  • 2006

Quantified Score

Hi-index 0.00

Visualization

Abstract

We describe a scheme for implementing dynamic casts suitable for systems where the performance and predictability of performance is essential. A dynamic cast from a base class to a derived class in an object-oriented language can be performed quickly by having the linker assign an integer type ID to each class. A simple integer arithmetic operation verifies whether the cast is legal at run time. The type ID scheme presented uses the modulo function to check that one class derives from another. A 64-bit type ID is sufficient to handle class hierarchies of large size at least nine levels of derivation deep. We also discuss the pointer adjustments required for a C++ dynamic_cast. All examples will be drawn from the C++ language. Copyright © 2005 John Wiley & Sons, Ltd.