next up previous
Konvertierungen und RTTI

Beispiel DCastExp

 
  unsigned diff = unsigned(pQ) - unsigned(pA);

  cout << hex << "&c  = " << (&c) << endl
              << "pA  = " << (pA) << endl
              << "pB  = " << (pB) << endl
              << "pS  = " << (pS) << endl
              << "pC  = " << (pC) << endl
              << "pQ  = " << (pQ) << endl
       << dec << "Differenz (dec) = " << diff << endl
              << "sizeof(c)       = " << sizeof(c) << endl;

  A a(2);
  pA = &a;
  A& ra = a;

  pC = dynamic_cast<C*>(pA);              // Error: Casting down to C
  if(!pC)
  {
    cout << "Downcast-Error: But O.K." << endl;
    cout << "The real type of (*pA) is " << typeid(*pA).name() << endl;
  }
  else
    cout << (*pC) << endl;                // ???

  try {
    C& rc = dynamic_cast<C&>(ra);         // Error: Casting down to C
  }
  catch(bad_cast& rbc) {
    cout << "Exception handled: " << rbc.what() << endl;
  }

  return 0;
}


next up previous