next up previous
 Upcasts und Downcasts

 
// File: UCastImp.cc

#include <PROTECT/SiOpCall.h>
int main()
{
  C c(1, 'A', 3.3);
  cout << c << endl;
  A* pA = &c;            // Upcast
  cout << (*pA) << endl;
  B* pB = &c;            // Upcast
  cout << (*pB) << endl;
  S* pS = &c;            // Upcast
  cout << (*pS) << endl;

// Fuer den Compiler ist es "UP";
// fuer uns ist es "DOWN":
// cannot cast up from virtual baseclass `S'
// C* pC = (C*)pS;       // Downcast
  C* pC = (C*)pA;        // Downcast
  cout << (*pC) << endl;
  B* pQ = (B*)(C*)pA;    // 'Quer'cast
  cout << (*pQ) << endl;
  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;
  pC = (C*)pA;           // Error: Casting down to C
  cout << (*pC) << endl; // ??? virtuelle Meth., evtl. Glueck
  return 0;
}


next up previous