performs safe downcasting for polymorphic types. RTTI is useful for type specialization in code and debugging. It respects polymorphism by providing the underlying object type. dynamic_cast<> checks for valid inheritance and returns null/throws exception on failure, while typeid() only checks declared types. RTTI has some limitations like compiler-specific name formats and requires polymorphism."> performs safe downcasting for polymorphic types. RTTI is useful for type specialization in code and debugging. It respects polymorphism by providing the underlying object type. dynamic_cast<> checks for valid inheritance and returns null/throws exception on failure, while typeid() only checks declared types. RTTI has some limitations like compiler-specific name formats and requires polymorphism.">
Introduction To RTTI: Jonathan Hoyle Eastman Kodak 11/30/00
Introduction To RTTI: Jonathan Hoyle Eastman Kodak 11/30/00
Introduction To RTTI: Jonathan Hoyle Eastman Kodak 11/30/00
Jonathan Hoyle
Eastman Kodak
11/30/00
Overview
What is RTTI?
typeid( ) function
Polymorphism
dynamic_cast< > operation
RTTI Gotchas
Demo
What is RTTI?
Run-Time Type Identification
Dynamically determining an objects type
Two RTTI operations:
typeid( ) function
dynamic_cast< >
(generic types)
(polymorphic types)
typeid( ) function
Returns a type_info describing that type
typeid() can be used on any variable or type
name() returns the type name as a string
type_infos can be compared using the == and
!= operators
It is polymorphic-friendly
Must #include the following header:
#include <typeinfo.h>
typeid( ) Example #1
// Prints the type name to screen
template <class T>
void WhatAmI(T x)
{ cout << typeid(x).name() << endl; }
WhatAmI(1);
WhatAmI(1.0);
WhatAmI(Hi!);
MyClass
x;
WhatAmI(x);
// prints int
// prints double
// prints char *
typeid( ) Example #2
// Test if a given object is a basic numeric type
template <class T>
bool IsNumericType(T x)
{
if (typeid(x) == typeid(short))
return true;
if (typeid(x) == typeid(long))
return true;
if (typeid(x) == typeid(int))
return true;
if (typeid(x) == typeid(double)) return true;
...
return false;
}
Polymorphism
A class which declares or inherits a virtual
function is called a polymorphic class:
class Av
{
public:
virtual void foo();
};
class Bv : public Av
{
...
};
Polymorphism
Polymorphic classes call the intended virtual
function despite variable type:
class A
class B
: public A
class Av
class Bv : public Av
A *aPtr = new B;
Av *avPtr = new Bv;
// non-polymorphic class
// polymorphic class
aPtr->foo();
avPtr->foo();
// calls A::foo()
// calls Bv::foo()
: public A
class Av
class Bv : public Av
A *aPtr = new B;
Av *avPtr = new Bv;
class
class
class
class
ZCommunications
ZParallel : public ZCommunications
ZSCSI
: public ZCommunications
ZFirewire : public ZCommunications
{
{
{
{
...
...
...
...
};
};
};
};
typeid( ) Tips
You cannot determine the real type of an
object pointed to by a void *.
For non-polymorphic typed variables,
typeid() gives info on the variable type.
For polymorphic typed variables, typeid()
gives info on the underlying real type.
You cannot determine the name of an
objects base class.
dynamic_cast< >
dynamic_cast< > is used to cast one
polymorphic type to another type within its
inheritance chain
dynamic_cast< > performs a safe down
cast.
dynamic_cast< > operations must be used
on polymorphic pointers or references only.
class
class
class
class
{
{
{
{
...
...
...
...
};
};
};
};
class ZCommunications
class ZSCSI
: public ZCommunications
class ZSCSI_TSP : public ZSCSI
{ ... };
{ ... };
{ ... };
RTTI Gotchas
For typeid(), always #include
<typeinfo.h>.
Demo