Run-time Type Information
Object Pascal has language constructs dealing with RTTI. Some have C++ counterparts. These are listed in the following table:
Examples of RTTI mappings from Object Pascal to C++
|Object Pascal RTTI||C++ RTTI|
if Sender is TButton (* ... *)
if ((dynamic_cast<TButton*>(Sender)) != NULL) // dynamic_cast returns NULL on failure
b := Sender as TButton; (* raises an exception on failure *)
TButton& ref_b = dynamic_cast<TButton&>(*Sender); // throws an exception on failure
In the preceding table, ClassName is a TObject method that returns a string containing the name of the actual type of the object, regardless of the type of the declared variable. Other RTTI methods introduced in TObject do not have C++ counterparts. These are all public and are listed here:
- ClassInfo returns a pointer to the run-time type information (RTTI) table of the object type.
- ClassNameIs determines whether an object is of a specific type.
- ClassParent returns the type of the immediate ancestor of the class. In the case of TObject, ClassParent returns nil because TObject has no parent. It is used by the is and as operators, and by the InheritsFrom method.
- ClassType dynamically determines the actual type of an object. It is used internally by the Object Pascal is and as operators.
- FieldAddress uses RTTI to obtain the address of a published field. It is used internally by the steaming system.
- InheritsFrom determines the relationship of two objects. It is used internally by the Object Pascal is and as operators.
- MethodAddress uses RTTI to find the address of a method. It is used internally by the steaming system.
Some of these methods of TObject are primarily for internal use by the compiler or the streaming system.