Show: Delphi
C++
Display Preferences
bad_cast class
From RAD Studio
Go Up to typeinfo.h Index
Contents |
Header File
typeinfo.h
Description
When dynamic_cast fails to make a cast to reference, the expression can throw bad_cast class. Note that when dynamic_cast fails to make a cast to pointer type, the result is the null pointer.
Example
How to perform run-time type identification
#include <iostream> #include <typeinfo.h> using std::cout; class __rtti Alpha { virtual void func() {}; // This makes Alpha a polymorphic class type. }; class B : public Alpha {}; int main(void) { B Binst; // Instantiate class B B *Bptr; // Declare a B-type pointer Bptr = &Binst; // Initialize the pointer // THESE TESTS ARE DONE AT RUN TIME // Ask "WHAT IS THE TYPE FOR *Bptr?" if (typeid( *Bptr ) == typeid( B ) ) cout << "Name is " << typeid( *Bptr).name(); if (typeid( *Bptr ) != typeid( Alpha ) ) cout << "\nPointer is not an Alpha-type."; return 0; }
Program output:
Name is B Pointer is not an Alpha-type.