SHOW:
|
|
- or go back to the newest paste.
| 1 | - | #include <iostream> |
| 1 | + | |
| 2 | {
| |
| 3 | - | using namespace std; |
| 3 | + | |
| 4 | void x() | |
| 5 | {
| |
| 6 | //... | |
| 7 | } | |
| 8 | }; | |
| 9 | ||
| 10 | //for the first attempt | |
| 11 | class A1 :public X {};
| |
| 12 | class B1 :public X {};
| |
| 13 | class C1 :public A1, public B1 {};
| |
| 14 | ||
| 15 | //for the second attempt | |
| 16 | class A2 :public X {};
| |
| 17 | class B2 :public X {};
| |
| 18 | class C2 :public A2, public B2 {};
| |
| 19 | ||
| 20 | int main(int argc,char *argv[]) | |
| 21 | {
| |
| 22 | //first attempt | |
| 23 | A1 *a1=new A1(); | |
| 24 | B1 *b1=new B1(); | |
| 25 | C1 *c1=new C1(); | |
| 26 | ||
| 27 | a1->x(); | |
| 28 | b1->x(); | |
| 29 | c1->x(); | |
| 30 | ||
| 31 | X *ax1=a1; | |
| 32 | X *bx1=b1; | |
| 33 | X *cx1=c1; | |
| 34 | ||
| 35 | ax1->x(); | |
| 36 | bx1->x(); | |
| 37 | cx1->x(); | |
| 38 | ||
| 39 | a1=(A1 *)ax1; //Error: cannot convert pointer to base class "X" to pointer to derived class "A1" -- base class is virtual | |
| 40 | b1=(B1 *)bx1; //Error: cannot convert pointer to base class "X" to pointer to derived class "B1" -- base class is virtual | |
| 41 | c1=(C1 *)cx1; //Error: cannot convert pointer to base class "X" to pointer to derived class "C1" -- base class is virtual | |
| 42 | ||
| 43 | //second attempt | |
| 44 | A2 *a2=new A2(); | |
| 45 | B2 *b2=new B2(); | |
| 46 | C2 *c2=new C2(); | |
| 47 | ||
| 48 | a2->x(); | |
| 49 | b2->x(); | |
| 50 | c2->x(); //Error: "C2::x" is ambiguous | |
| 51 | ||
| 52 | X *ax2=a2; | |
| 53 | X *bx2=b2; | |
| 54 | X *cx2=c2; //Error: base class "X" is ambiguous | |
| 55 | ||
| 56 | ax2->x(); | |
| 57 | bx2->x(); | |
| 58 | cx2->x(); | |
| 59 | ||
| 60 | a2=(A2 *)ax2; | |
| 61 | b2=(B2 *)bx2; | |
| 62 | c2=(C2 *)cx2; //Error: derived class "C2" contains more than one instance of class "X" | |
| 63 | ||
| 64 | return 0; | |
| 65 | } |