- What is RTTI ?
RTTI refers to run time type Information/identification.
Where it is used ?
Run-time type information can apply to simple data types, such as integers and characters, or to generic types
Why it is required ?
RTTI is used to know the type of an object at runtime.
- Where it is used ?
- How RTTI is used in CPP
In C++ RTTI can be achieved with the help of two ways
1.typeid operator is defined in header file.
2.dynamic_cast <> operation
The C++ run-time type information permits performing safe typecasts and manipulate type information at run time.
If CShape is a base class and CCircle is a class drived from CShape. Then casting CShape object to CCircle object is said to be the downcasting and casting CCircle object to CShape object is said to be the upcasting.
- Sample Code
NOTE:
1.RTTI doesn't work with non ploymorphic classes.The classes which doesn't have the virtual function.
2.RTTI doesn't work with void pointers.
Others :
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).
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.
No comments:
Post a Comment