#include #include #include //for the bad_cast exception in line 28 #include "grandchild.h" using namespace std; int main() { base b(10); middle m(10, 20); grandchild g(10, 20, 30); const base *const a[] = {&b, &m, &g, 0}; const size_t n = sizeof a / sizeof a[0]; for (size_t i = 0; i < n; ++i) { cout << "a[" << i << "]: "; if (a[i] == 0) { cout << "zero pointer"; } else { cout << *a[i] << " "; try { const middle& r = dynamic_cast(*a[i]); r.newfunc(); } catch (const bad_cast& b) { cout << "caught bad_cast " << b.what(); } } cout << "\n"; } return EXIT_SUCCESS; }