Run-time type information

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer programming, RTTI (Run-Time Type Information, or Run-Time Type Identification[citation needed]) refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic types. This is a C++ specialization of a more general concept called type introspection. Similar mechanisms are also known in other programming languages, such as Delphi (Object Pascal).

In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was frequently misused.[1]

Features[edit]

The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.

The C++ run-time type information permits performing safe typecasts and manipulate type information at run time.

RTTI is available only for classes which are polymorphic, which means they have at least one virtual method. In practice, this is not a limitation because base classes must have a virtual destructor to allow objects of derived classes to perform proper cleanup if they are deleted from a base pointer.

RTTI is optional with some compilers; the programmer can choose at compile time whether to include the function. There may be a resource cost to making RTTI available even if the program does not use it.

C++ Example[edit]

/* A base class pointer can point to objects of any class which is derived 
 * from it. RTTI is useful to identify which type (derived class) of object is 
 * pointed to by a base class pointer.
 */
 
#include <iostream>
 
class Base
{
public:
    Base() { } 
    virtual ~Base() { } 
 
    virtual void hello() 
    {
        std::cout << "in Base";
    }
};
 
class Derived : public Base
{
public:
    void hello() 
    {
        std::cout << "in Derived";
    }
};
 
int main()
{
    Base* basePointer = new Derived();
    Derived* derivedPointer = NULL;
 
    //To find whether basePointer is pointing to Derived type of object
    derivedPointer = dynamic_cast<Derived*>(basePointer);
 
    if (derivedPointer != NULL)
    {
        std::cout << "basePointer is pointing to a Derived class object"; //Identified
    }
    else
    {
        std::cout << "basePointer is NOT pointing to a Derived class object";
    }
 
    //Requires virtual destructor 
    delete basePointer;
    basePointer = NULL;
 
    return 0;
}

An instance where RTTI is used is illustrated below:

class Base
{
public:  
    virtual ~Base(){}
};
 
class Derived : public Base
{
public:
    Derived() {}
    virtual ~Derived() {}
 
    int compare (Derived& ref);
};
 
int myComparisonMethodForGenericSort (Base& ref1, Base& ref2)
{
    Derived& d = dynamic_cast<Derived&>(ref1); //RTTI used here
    //Note: If the cast is not successful, RTTI enables the process to throw a bad_cast exception
 
    return d.compare (dynamic_cast<Derived&>(ref2));
}

See also[edit]

References[edit]

  1. ^ Bjarne Stroustrup. "A History of C++: 1979—1991". p. 50. Retrieved 2009-05-18.