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

// COMMENT OBTENIR LES INFORMATIONS DE TYPE A L'EXECUTION.
#include <iostream>
#include <typeinfo.h>

using std::cout;

class __rtti Alpha {
   virtual void func() {};  // Cela rend Alpha en un type de classe polymorphique.
};

class B : public Alpha {};

int main(void)
{
   B Binst;           // Instancier la classe B
   B *Bptr;           // Déclarer un pointeur de type B
   Bptr = &Binst;     // Initialiser le pointeur

   // CES TESTS SONT EFFECTUES A L'EXECUTION

   // Demandez "QUEL EST LE TYPE DE *Bptr?"

   if (typeid( *Bptr ) == typeid( B ) )
       cout << "Le nom est " << typeid( *Bptr).name();

   if (typeid( *Bptr ) != typeid( Alpha ) )
       cout << "\nLe pointeur n'est pas du type Alpha";

   return 0;
}

Sortie du programme

Le nom est B
Le pointeur n'est pas dy type Alpha.