// typeid #include #include #include #include using namespace std; class CBase { virtual int dummy() { return 0;} }; class CDerived: public CBase { int a; }; int main () { int * a,b; a=0; b=0; if (typeid(a) != typeid(b)) { cout << "a and b are of different types:\n"; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; } cout << "calling typeid(NULL)" << endl; try { const char *bad = NULL; const char *foo = typeid( *bad).name(); cout << "typeid( *(char*)NULL).name() = " << foo << endl; } catch (bad_typeid bt) { cout << "bad_type exception caught what=" << bt.what() << endl; } catch (exception e) { cout << "exception caught " << e.what() << endl; } catch (...) { cout << "something bad happened" << endl; } try { const int *bad = NULL; const char *foo = typeid( *bad).name(); cout << "typeid( *(int*)NULL).name() = " << foo << endl; } catch (bad_typeid bt) { cout << "bad_type exception caught what=" << bt.what() << endl; } catch (exception e) { cout << "exception caught " << e.what() << endl; } catch (...) { cout << "something bad happened" << endl; } try { const long *bad = NULL; const char *foo = typeid( *bad).name(); cout << "typeid( *(long*)NULL).name() = " << foo << endl; } catch (bad_typeid bt) { cout << "bad_type exception caught what=" << bt.what() << endl; } catch (exception e) { cout << "exception caught " << e.what() << endl; } catch (...) { cout << "something bad happened" << endl; } try { const double *bad = NULL; const char *foo = typeid( *bad).name(); cout << "typeid( *(double*)NULL).name() = " << foo << endl; } catch (bad_typeid bt) { cout << "bad_type exception caught what=" << bt.what() << endl; } catch (exception e) { cout << "exception caught " << e.what() << endl; } catch (...) { cout << "something bad happened" << endl; } try { const CBase *bad = NULL; const char *foo; cout << "calling typeid( *(CBase*)(NULL)).name()" << endl; foo = typeid( *bad).name(); cout << "typeid( *(CBase*)(NULL)).name() = " << foo << endl; } catch (bad_typeid bt) { cout << "bad_type exception caught what=" << bt.what() << endl; } catch (exception e) { cout << "exception caught " << e.what() << endl; } catch (...) { cout << "something bad happened" << endl; } return 0; }