|
Implementation notes
Runtime type identification
|
|
A reasonably usable runtime type identification mechanism is provided. This requires a limited amount of user intervention in that classes supporting RTTI have to provide some static data and virtual functions to support the mechanism. Providing this scaffolding is made relatively straightforward by the use of a collection of macros accessible via the standard typeinfo header file. For example:
// somefile.h
#include "typeinfo.h"
class Base
{
public:
// etc...
RTTI_SCAFFOLDING_DECL // Scaffolding macro
};
class Derived : public Base
{
public:
// etc...
RTTI_SCAFFOLDING_DECL // Scaffolding macro
};
// somefile.c++
#include "iostream.h"
#include "somefile.h"
#include "exception.h"
// definition of Base members ...
RTTI_SCAFFOLDING_DEFN_0(Base)
// definition of Derived members ...
RTTI_SCAFFOLDING_DEFN_1(Derived, Base)
void some_fn(Base* base)
{
Derived* derived;
if((derived = PTR_dynamic_cast(Derived, base)) == 0)
cout << "base was not a pointer to a Derived" << endl;
else
cout << "base was a pointer to a Derived" << endl;
try
{
Derived& derived_ref = REF_dynamic_cast(Derived, *base);
}
BEGIN_HANDLERS
catch(bad_cast ex)
{
cout << "*base was not a reference to a Derived" << endl;
}
END_HANDLERS
}
Examples of runtime type identification can be found in test.c++.trtti
.