Next: Creating
typeids Up: RTTI
Support for Previous: Adding
RTTI to
Additional RTTI Tools
In Section 4.1 we have introduced
the most important tools that the application programmer can use to exploit
RTTI. This section introduces some additional, less important tools which
refine the features made available by the first set of tools or are more
convenient to use (e.g. less verbose). Assume the same notations as in
Section 4.1.
- const char* STATIC_TYPE_NAME(T): Returns the textual name of
class T (shorthand for STATIC_TYPE_INFO(T).getname()).
- const char* TYPE_NAME(p): Returns the textual class name of
the class of *p (shorthand for TYPE_INFO(p).getname()).
- int DYN_CAST(typeid t,p): Given a pointer p, returns
1 if it can be cast to the type described by t, else 0. This macro
is similar to PTR_CAST but offers more freedom. While PTR_CAST
lets you vary the pointer you cast (but still keeps the type you cast to
fixed, since given at compile time), DYN_CAST lets you vary both
the pointer and the type (represented now by a typeid) at run-time. DYN_CAST
is therefore useful when you want to check that a pointer selected at run-time
casts to a type selected at run-time. Of course, it has a drawback: while
PTR_CAST returns a typed C++ pointer, DYN_CAST can only return
a flag (1/0) since one can not obtain typed pointers unless the
type is precised at compile-time (see also Section 4).
- int typeid::can_cast(typeid): Similar to DYN_CAST, this
method returns 1 if the typeid argument can be cast to 'this', else 0.
This method can be useful to check casting directly on typeids rather than
on pointers and C++ types.
- typeid::typeid(const typeid&),... : The typeid class provides
all the usual copy constructor and assignment operator semantics. In other
words, typeids can be copy constructed, passed and returned by value or
by reference, assigned to and so on. This makes the typeid class as simple
to use as a basic C++ data type.
Next: Creating
typeids Up: RTTI
Support for Previous: Adding
RTTI to
Alexandru Telea