bad_cast class

De RAD Studio
Aller à : navigation, rechercher

Remonter à typeinfo.h - Index


Header File

typeinfo.h

Description

When dynamic_cast fails to make a cast to reference, the expression can throw bad_cast. Note that when dynamic_cast fails to make a cast to pointer type, the result is the null pointer.

Example

#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;
}


Sortie du programme

Name is B
Pointer is not an Alpha-type.