Run-time type information
Encyclopedia
In programming, RTTI refers to a C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 system that makes information about an object's data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

 available at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic objects. This is a C++ implementation of a more generic concept called reflection
Reflection (computer science)
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime....

 or, more specifically, type introspection
Type introspection
In computing, type introspection is a capability of some object-oriented programming languages to determine the type of an object at runtime. This is a notable capability of the Objective-C language, and is a common feature in any language that allows object classes to be manipulated as first-class...

.

In the original C++ design, Bjarne Stroustrup
Bjarne Stroustrup
Bjarne Stroustrup ; born December 30, 1950 in Århus, Denmark) is a Danish computer scientist, most notable for the creation and the development of the widely used C++ programming language...

 did not include run-time type information, because he thought this mechanism was frequently misused.

Features

The dynamic cast
Dynamic cast
In the C++ programming language, the dynamic_cast operator is a part of the run-time type information system that performs a typecast. Unlike an ordinary C-style typecast, a type safety check is performed at runtime, and if the types are not compatible, an exception will be thrown or a null...

<>
operation and typeid
Typeid
In C++, the typeid keyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program...

operator in C++ are part of RTTI.

The C++ run-time type information permits performing safe typecast
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

s 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


/* 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 cls pointer.
*/
  1. include


class abc // base class
{
public:
virtual ~abc { }
virtual void hello
{
std::cout << "in abc";
}
};

class xyz : public abc
{
public:
void hello
{
std::cout << "in xyz";
}
};

int main
{
abc *abc_pointer = new xyz;
xyz *xyz_pointer;

// to find whether abc_pointer is pointing to xyz type of object
xyz_pointer = dynamic_cast(abc_pointer);

if (xyz_pointer != NULL)
{
std::cout << "abc_pointer is pointing to a xyz class object"; // identified
}
else
{
std::cout << "abc_pointer is NOT pointing to a xyz class object";
}

// needs virtual destructor
delete abc_pointer;

return 0;
}


----

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(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(ref2));
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK