Run-time type information

From Wikipedia, the free encyclopedia

In programming, Runtime Type Information (RTTI, RunTime Type Identification) means keeping information about an object's datatype in memory at runtime. Some implementations may be limited to keeping the inheritance tree while others may also include information about objects' methods and attributes

Run Time Type Information is a computer programming term used to identify that a given language has the ability to determine the type of a program's object/variable at runtime.

Although available in several computer languages, RTTI, as a term, is typically used in relation to C++. In order for the dynamic_cast<> operation or the typeid operator to work in C++, RTTI must be enabled.

[edit] Example in C++

An instance where RTTI is used is illustrated below:


class base {
  virtual ~base(){}
};

class derived : public base {
  public:
    virtual ~derived(){}
    int compare (derived &ref);
};

int my_comparison_method_for_generic_sort (base &ref1, base &ref2)
{
  derived & d = dynamic_cast<derived &>(ref1); // rtti used here
  // rtti enables the process to throw a bad_cast exception
  // if the cast is not successful
  return d.compare (dynamic_cast<derived &>(ref2));
}