T
Ich hab auch BCB 5 Ent, bei mir ist folgendes Beispiel:
// HOW TO MAKE DYNAMIC CASTS
// This program must be compiled with the -RT (Generate RTTI) option.
#include <iostreamstd::>
#include <typeinfo.h>
class Base1
{
// In order for the RTTI mechanism to function correctly,
// a base class must be polymorphic.
virtual void f(void) { /* A virtual function makes the class polymorphic */ }
};
class Base2 { };
class Derived : public Base1, public Base2 { };
int main(void) {
try {
Derived d, *pd;
Base1 *b1 = &d;
// Perform a downcast from a Base1 to a Derived.
if ((pd = dynamic_cast<Derived *>(b1)) != 0) {
std::cout << "The resulting pointer is of type "
<< typeid(pd).name() << std::endl;
}
else throw Bad_cast();
// Attempt cast across the hierarchy. That is, cast from
// the first base to the most derived class and then back
// to another accessible base.
Base2 *b2;
if ((b2 = dynamic_cast<Base2 *>(b1)) != 0) {
cout << "The resulting pointer is of type "
<< typeid(b2).name() << endl;
}
else throw Bad_cast();
}
catch (Bad_cast) {
cout << "dynamic_cast failed" << endl;
return 1;
}
catch (...) {
cout << "Exception handling error." << endl;
return 1;
}
return 0;
}
Ich hab es aber nicht getestet...