1 // { dg-do run } 2 // { dg-options "-w" } 3 // test of rtti of single inheritance and multiple inheritance with 4 // virtual inheritance 5 // dynamic casting 6 7 #include <typeinfo> 8 9 extern "C" { 10 int printf(const char *, ...); 11 void exit(int); 12 } 13 14 class X { 15 public: 16 int xi; f()17 virtual int f() {}; 18 }; 19 20 class Y : public virtual X { 21 short ys; 22 }; 23 24 class Z : public virtual Y { 25 int zi; 26 }; 27 28 Z z; 29 Y y; 30 Y *yp = &z; 31 X *xp = &z; 32 Z *zp = &z; 33 34 class A { 35 public: 36 int Ai; a()37 virtual int a() {}; 38 }; 39 40 class B { 41 public: 42 int Bi; g()43 virtual int g() {}; 44 }; 45 46 class D : public virtual A, private B { 47 int Di; 48 }; 49 50 class E : public virtual D, public B { 51 int Ei; 52 }; 53 54 class F : public E, public virtual D { 55 int Fi; 56 }; 57 58 D d; 59 A *ap = &d; 60 B *bp = (B *)&d; 61 F f; 62 F *fp = &f; 63 A *aap = &f; 64 D *dp = &f; 65 E *ep = &f; 66 B *bbp = (B *)dp; 67 68 void *vp = zp; 69 70 /* 71 void error (int i) 72 { 73 printf("FAIL\n"); 74 exit(i); 75 } 76 */ 77 error(int i)78void error (int i) 79 { 80 exit(i); 81 } 82 main()83int main () 84 { 85 vp = (void *)0; 86 87 vp = dynamic_cast<Y *> (&z); 88 if (vp == 0) error(11); 89 90 vp = dynamic_cast<Z *> (yp); 91 if (vp == 0) error(11); 92 93 vp = dynamic_cast<X *> (yp); 94 if (vp == 0) error(12); 95 96 vp = dynamic_cast<D *> (dp); 97 if (vp != (void *)dp) error(21); 98 99 // Ill-formed: dynamic_cast to private or ambiguous base 100 // vp = dynamic_cast<B *> (dp); 101 // if (vp == (void *)dp) error(21); 102 103 // vp = dynamic_cast<B *> (fp); 104 // if (vp == (void *)bbp) error(22); 105 106 vp = dynamic_cast<void *> (aap); 107 if (vp != (void *)fp) error(23); 108 109 vp = dynamic_cast<B *> (aap); 110 if (vp == (void *)bbp) error(24); 111 112 } 113