2 // Author: Alfred Miniarik <a8601248@unet.univie.ac.at>
3 // test of dynamic_cast
4 // runtime detecting of nonpublic
5 // inheritance within a cast
6 // and therefor failing with result 0.
8 extern "C" void abort();
9 extern "C" int printf (const char *, ...);
11 static int errors = 0;
14 printf("Error %i\n",i);
20 // 1.1. single inheritance case
22 struct A {virtual ~A(){}};
32 class CCC : protected B {};
33 class DDD : protected CCC {};
39 if(dynamic_cast<D*> ((A*)&d)) error(1);
40 if(dynamic_cast<D*> ((B*)&d)) error(2);
41 if(&d != dynamic_cast<D*> ((C*)&d)) error(3); //counter example
42 if(dynamic_cast<C*> ((B*)&d)) error(4);
45 if(dynamic_cast<DD*> ((A*)&dd)) error(5);
46 if(dynamic_cast<DD*> ((B*)&dd)) error(6);
49 if(dynamic_cast<DDD*> ((A*)&ddd)) error(7);
50 if(dynamic_cast<DDD*> ((B*)&ddd)) error(8);
51 if(dynamic_cast<CCC*> ((B*)&ddd)) error(9);
54 // 1.2. multiple inheritance case
55 // 1.2.1. all bases are public
58 struct EE : CC, D {}; //Will search in reverse order.
64 if(dynamic_cast<E*> ((A*)(D*)&e)) error(10);
65 if(dynamic_cast<E*> ((B*)(D*)&e)) error(11);
66 if(&e != dynamic_cast<E*> ((C*)(D*)&e)) error(12); //counter example
67 if(&e != dynamic_cast<E*> ((B*)(CC*)&e)) error(13); //counter example
68 if((CC*)&e != dynamic_cast<CC*> ((B*)(CC*)&e)) error(14); //counter example
71 if(dynamic_cast<EE*> ((A*)(D*)&ee)) error(15);
72 if(dynamic_cast<EE*> ((B*)(D*)&ee)) error(16);
73 if(&ee != dynamic_cast<EE*> ((C*)(D*)&ee)) error(17); //counter example
74 if(&ee != dynamic_cast<EE*> ((B*)(CC*)&ee)) error(18); //counter example
75 if((CC*)&ee != dynamic_cast<CC*> ((B*)(CC*)&ee)) error(19); //counter example
78 // 1.2.2 one or more branches are nonpublic
80 struct X : private BB, E {};
81 struct Y : AA, private B {};
89 if(&x != dynamic_cast<X*>((B*)(CC*)(E*)&x)) error(20); //counter example
91 if(dynamic_cast<XX*>((B*)(CC*)(E*)&xx)) error(21);
93 if(dynamic_cast<Y*>((B*)&y)) error (22);
94 if(dynamic_cast<Y*>((A*)(B*)&y)) error (23);
99 struct J {virtual ~J(){};};
100 struct K : CC, private J {};
107 if(dynamic_cast<CC*> ((B*)(D*)&e)) error(24);
108 if((CC*)&e != dynamic_cast<CC*> ((C*)(D*)&e)) error(25); //counter example
110 if(dynamic_cast<J*> ((B*)&k)) error(26);
112 if(dynamic_cast<J*> ((CC*)&kk)) error(27);
122 return errors ? 1 : 0;