Next: The Concept of Up: A General-Purpose Run-Time Type Previous: A General-Purpose Run-Time Type

Introduction

  C++ is a widely spread, general-purpose programming language. One of its main advantages is its strong typing: any C++ object or variable has a well-defined type, which is declared at compile-time. This offers the great advantage of compile-time type-checking, which can trap many programming errors or inconsistencies and leads to a well-structured program design.

In contrast with this approach, interpreted languages offer the facility to declare new types at run-time and, consequently, run-time objects of these new types. This facility can be useful when an application doesn't know beforehand (i.e. at compile-time) which is the complete set of types and/or objects it will need during its execution. A typical example is an 'open' object-oriented application which consists of a pre-compiled 'kernel' and a set of user-written 'class libraries' which are loaded and used by the kernel. The idea is to leave this set open, i.e. to allow the user to write new classes (derived or not from the initial classes) and make them available to the kernel without having to recompile the latter, or to create instances of these classes at run-time. The kernel should be able to become aware of these new types and instantiate them at run-time.

Statically typed languages like C++ must resolve all operations involving types (e.g. casting and creation of objects of given types) at compile time. It is however possible to partially simulate the freedom offered by dynamically-typed languages like Objective-C or Java by introducing the concept of type variables or typeids (see [Stroustrup, 1991] for an introduction to typeids for C++). The first step in this respect is done by some C++ compilers which support some run-time type operations (e.g. pointer casting) However, there are still many compilers which lack such support or provide it only partially. Another approach is taken by application libraries which provide their own run-time type information system for their classes. An example is the Open Inventor C++ library [Wernecke, 1994]. Yet another example is the ROOT data analysis system [Brun et al., 1995]. The main drawback of many of these software solutions to run-time type information is that they impose strong constraints on the application classes which desire to benefit from it (e.g. inherit from a specific base class).

This paper presents a run-time type information (RTTI) system which offers most of the features already presented: type variables (typeids), run-time type information about any program object, run-time pointer casting in its most general form and run-time object creation from typeids. The presented system comes as a C++ header and a C++ source file which implement a set of simple tools that can be used to transparently add RTTI to any C++ class in an application. The system is written in standard C++ as described by [Stroustrup, 1991] and should compile with any C++ compiler which complies to that standard.



next up previous index
Next: The Concept of Up: A General-Purpose Run-Time Type Previous: A General-Purpose Run-Time Type



Alexandru Telea