Вы находитесь на странице: 1из 14

Run Time Type Identification

(RTTI)

OOPS using C++ Page 1


Objectives

In this session you will learn to:


• Define RTTI
• Describe Application of RTTI
• Use dynamic_cast operator

OOPS using C++ Page 2


RTTI

• In polymorphic languages such as C++, there can be


situations in which the type of an object is unknown at
compile time, i.e., when the program is written.

• It is not always possible to know in advance what type of


object will be pointed to by a base class pointer at any
given point in time.

• This determination must be made at runtime, using runtime


type identification.

OOPS using C++ Page 3


What is RTTI?

• Mechanism that allows the type of an object to be


determined during program execution.

• Used only with polymorphic classes (i.e. those which


have a virtual function in the base class) to find the exact
type of an object when you have a pointer or reference to
the base type.

• In the absence of polymorphism, the static type


information is used

OOPS using C++ Page 4


RTTI
• RTTI is possible only with virtual functions and base class
pointers. While using virtual functions, you must have
access to the base class source code.

• If the base class is a part of a library and does not contain


the virtual function that you need, you are stuck up.

• At such times you should use RTTI. You can derive a new
class from the base class and add your extra member
function to it.

• Then, you can detect your particular type (using RTTI)


and call the member function.
OOPS using C++ Page 5
C++ RTTI support

C++ provides two ways to obtain the information about the


object class at runtime. These are:

• Using typeid() operator and type_info class.

• Using dynamic_cast operator

OOPS using C++ Page 6


The type_info class

It defines the following public members:

• bool operator==(const type_info &ob);


• bool operator!=(const type_info &ob);
• bool before(const type_info &ob);
• const char *name();

OOPS using C++ Page 7


A Simple Application of RTTI
• #include<iostream>
• using namespace std;
• class Figure
• {public:
• virtual void draw( ) = 0; };

• class Rectangle : public Figure


• {public:
• void draw( )
• { cout << “Rectangle’s draw\n”; } } ;

• class Circle : public Figure


• {public:
• void draw( )
• { cout << “Circle’s draw\n”; } };

OOPS using C++ Page 8


A Simple Application of RTTI
• Class Triangle : Public Figure
• {
• public:
• void draw( )
• { cout “Triangle’s draw\n”; } };
• Figure* factory( ) // a factory for objects derived from Figure
• {switch( rand( ) % 3)
• {case 0 : return new Rectangle;
• case 1 : return new Circle;
• case 2 : return new Triangle; }}
• int main( )
• {
• Figure *ptr; // pointer to base class
• int i;

OOPS using C++ Page 9


A Simple Application of RTTI

• int r, c, t;
• for (i = 0; i < 10, i + +) // generate and count objects
• {ptr = factory( ); // generate an object
• cout << “Object is “ << typeid(*ptr).name( ) << endl;
• if (typeid(*ptr) == typeid(Rectangle) r++;
• if (typeid(*ptr) == typeid(Circle) c++;
• if (typeid(*ptr) == typeid(Triangle) t++; }
• cout << “figures generated:\n”;
• cout << “rectangles: “ << r << endl;
• cout << “circles: “ << c << endl;
• cout << “triangles: “ << t << endl;
• return 0;
• }

OOPS using C++ Page 10


The dynamic_cast Operator

• The purpose of dynamic_cast is to perform casts on


polymorphic types.

• For e.g., given two polymorphic classes B and D, with D


derived from B, a dynamic cast can always cast a D*
pointer into a B* pointer.

• This is because a base pointer can always point to a


derived object.

OOPS using C++ Page 11


The dynamic_cast Operator

• But a dynamic_cast can cast a B* pointer to a D* pointer


only if the object being pointed to actually is a D object.

• If the cast fails, then dynamic_cast evaluates to NULL if


the cast involves pointers.

• If a dynamic_cast on reference types fails, a bad_cast


exception is thrown.

OOPS using C++ Page 12


Dynamic_cast -example

Base *bp, bobj;


Derived *dp, dobj;

bp=&dobj; //base pointer points to derived object


dp= dynamic_cast<Derived *> (bp);
if (dp) cout<<“Cast OK”;

bp=&bobj;
dp= dynamic_cast<Derived *> (bp);
if (!dp) cout<<“Cast fails”;

OOPS using C++ Page 13


Summary

In this session you learnt to:


• Define RTTI
• Describe Application of RTTI
• Use dynamic_cast operator

OOPS using C++ Page 14

Вам также может понравиться