next up previous
Konvertierungen und RTTI

Beispiel DCastExp

 
// File: DCastExp.cc

#include <typeinfo>
#include <PROTECT/SiOpCall.h>

int main()
{
  C c(1, 'A', 3.3);
  cout << c << endl;

  A* pA = static_cast<A*>(&c);            // Upcast
  cout << (*pA) << endl;

  B* pB = static_cast<B*>(&c);            // Upcast
  cout << (*pB) << endl;

  S* pS = static_cast<S*>(&c);            // Upcast
  cout << (*pS) << endl;

  C* pC  = dynamic_cast<C*>(pS);          // Downcast
  C* pCA = dynamic_cast<C*>(pA);          // Downcast
  C* pCB = dynamic_cast<C*>(pB);          // Downcast

  if((pC == pCA) && (pC == pCB) && (pC == &c))
    cout << (*pC) << endl;
  else
    cout << "Downcast-Error: Not O.K." << endl;

  B* pQ = dynamic_cast<B*>(pA);           // 'Quer'cast
  if(pQ == pB)
    cout << (*pQ) << endl;
  else
    cout << "Cast-Error: Not O.K." << endl;


next up previous