1 // Author: Alfred Miniarik <a8601248@unet.univie.ac.at>
2 // test of dynamic_cast
3 // runtime detecting of valid
4 // downcasts within nonpublic
7 extern "C" void abort();
8 extern "C" int printf (const char *, ...);
10 static int errors = 0;
14 printf("Error %i\n",i);
19 // 1.1 single inheritance case
21 struct A {virtual ~A(){};int i;};
22 struct B : A {int i;};
23 struct C : B {int i;};
27 struct E : D {int i;};
34 if((C*)&d != dynamic_cast<C*> ((A*)&d)) error(1);
35 if((C*)&d != dynamic_cast<C*> ((B*)&d)) error(2);
36 if((B*)&d != dynamic_cast<B*> ((A*)&d)) error(3);
39 if((C*)&e != dynamic_cast<C*> ((A*)&e)) error(4);
42 if((C*)&f != dynamic_cast<C*> ((B*)&f)) error(5);
43 if((B*)&f != dynamic_cast<B*> ((A*)&f)) error(6);
44 if((E*)&f != dynamic_cast<E*> ((D*)&f)) error(7);
45 if(dynamic_cast<E*> ((C*)&f)) error(8); //counter example
48 // 1.2 multiple inheritance case
56 if((B*)(F*)&g != dynamic_cast<B*> ((A*)(F*)&g)) error(9);
57 if(dynamic_cast<D*> ((A*)(F*)&g)) error(10);
58 if(dynamic_cast<G*> ((B*)(F*)&g)) error(11);
61 // 2. crosscast (always fail)
72 if(dynamic_cast<J*> ((I*)&l)) error(12);
73 if(dynamic_cast<J*> ((E*)&l)) error(13);
74 if(dynamic_cast<I*> ((J*)&l)) error(14);
83 return errors ? 1 : 0;