what is mean by RTTI in c++ and why we use that?

RTTI stands for Run Time Type Information.

C++ originally threw away all information on type during compilation. Hence unlike languages such as Java or C# where you have extensive reflection / introspection of types at runtime, when running a program you could not ask questions about types of objects - all such queries, such as they were (sizeof as standard) were resolved at compile time.

However ISO standard C++ introduced dynamic_cast. This is a type cast operation that is performed on pointer and reference types at runtime. Because it is a runtime operation it requires some - minimal - information about types and relationships between them - enter RTTI.

RTTI is created for classes - not instances of classes (although instances have to be able to access this information). This information has to be stored somewhere. There already existed a set of information for classes that declare virtual functions - the v-table of virtual function pointers. This fits in well with RTTIs major use - dynamic_cast - which can be used to safely downcast from a base class pointer or reference to a derived class pointer or reference and, even more usefully on those occasions you need it, to cross-cast from one base class pointer or reference to another - where multiple inheritance is in use. In such cases it would be usual to expect virtual functions to be used and therefore a v-table to be available and used already.

Hence RTTI is only created for classes that have a v-table, as this is where the information is stored. That is for classes that have at least one virtual instance member function - or a virtual destructor. This is in keeping with C++ aims of not paying for what you do not use - classes that do not declare virtual member functions are almost certainly not supposed to be used as part of a class hierarchy and so will almost certainly not require dynamic_cast and therefore not require any RTTI.

Note that because RTTI does incur an overhead it is often a feature that can be turned on or off by compiler options (the other C++ feature that is often controlled like this from compiler options is exception support).

The other part of RTTI is the typeid operator and the std::type_info class. An expression of the form


returns an object of the std::type_info class (include <typeinfo>).

The std::type_info class is somewhat limited in what you can do with its instances. They can be compared for equality/in-equality and have a before predicate member function to determine if one std::type_info instance represents a type that is notionally before another. What before means is implementation dependent and seems mainly for use in situations where the collation order is important such as storing std::type_info instances in associative or sorted containers. They also have a name member function that returns a zero terminated byte string. However this value is also implementation defined so could be the same string for different types - one possibility being that all instances return an empty string - so this may be less useful in the general case than it might at first appear.

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.