1 // prms-id: 3708
2 
3 extern "C" int printf (const char *, ...);
4 extern "C" int atoi (const char *);
5 
6 void *ptr;
7 
8 class A {
9 public:
A()10   A() { printf ("A is constructed.\n"); }
xx(int doit)11   virtual void xx(int doit) { printf ("A is destructed.\n"); }
12 };
13 
14 class A1 {
15 public:
A1()16   A1() { printf ("A1 is constructed.\n"); }
xx(int doit)17   virtual void xx(int doit) { printf ("A1 is destructed.\n"); }
18 };
19 
20 class B :  public A1, public virtual A {
21 public:
B()22   B() { printf ("B is constructed.\n"); }
xx(int doit)23   virtual void xx(int doit) {
24     printf ("B is destructed.\n");
25     A1::xx (1);
26     if (doit) A::xx (1);
27   }
28 };
29 
30 int num;
31 
32 class C : public virtual A, public B {
33 public:
C()34   C() { ++num; printf ("C is constructed.\n");
35       ptr = this;
36       }
xx(int doit)37   virtual void xx(int doit) {
38     --num;
39     if (ptr != this)
40       printf("FAIL\n%x != %x\n", ptr, this);
41     printf ("C is destructed.\n");
42     B::xx (0);
43     if (doit) A::xx (1);
44   }
45 };
46 
fooA(A * a)47 void fooA(A *a) {
48   printf ("Casting to A!\n");
49   a->xx (1);
50 }
fooA1(A1 * a)51 void fooA1(A1 *a) {
52   printf ("Casting to A1!\n");
53   a->xx (1);
54 }
55 
fooB(B * b)56 void fooB(B *b) {
57   printf ("Casting to B!\n");
58   b->xx (1);
59 }
60 
fooC(C * c)61 void fooC(C *c) {
62   printf ("Casting to C!\n");
63   c->xx (1);
64 }
65 
main(int argc,char * argv[])66 int main(int argc, char *argv[]) {
67   printf ("*** Construct C object!\n");
68   C *c = new C();
69 
70   int i = 0;
71 
72   printf ("*** Try to delete the casting pointer!\n");
73   switch (i)
74     {
75     case 0: fooA1(c);
76       break;
77     case 1: fooA(c);
78       break;
79     case 2: fooB(c);
80       break;
81     case 3: fooC(c);
82       break;
83     }
84 
85   return num!=0;
86 }
87