Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class X
- {
- public:
- void x()
- {
- //...
- }
- };
- //for the first attempt
- class A1 :public virtual X {};
- class B1 :public virtual X {};
- class C1 :public A1, public B1 {};
- //for the second attempt
- class A2 :public X {};
- class B2 :public X {};
- class C2 :public A2, public B2 {};
- int main(int argc,char *argv[])
- {
- //first attempt
- A1 *a1=new A1();
- B1 *b1=new B1();
- C1 *c1=new C1();
- a1->x();
- b1->x();
- c1->x();
- X *ax1=a1;
- X *bx1=b1;
- X *cx1=c1;
- ax1->x();
- bx1->x();
- cx1->x();
- a1=(A1 *)ax1; //Error: cannot convert pointer to base class "X" to pointer to derived class "A1" -- base class is virtual
- b1=(B1 *)bx1; //Error: cannot convert pointer to base class "X" to pointer to derived class "B1" -- base class is virtual
- c1=(C1 *)cx1; //Error: cannot convert pointer to base class "X" to pointer to derived class "C1" -- base class is virtual
- //second attempt
- A2 *a2=new A2();
- B2 *b2=new B2();
- C2 *c2=new C2();
- a2->x();
- b2->x();
- c2->x(); //Error: "C2::x" is ambiguous
- X *ax2=a2;
- X *bx2=b2;
- X *cx2=c2; //Error: base class "X" is ambiguous
- ax2->x();
- bx2->x();
- cx2->x();
- a2=(A2 *)ax2;
- b2=(B2 *)bx2;
- c2=(C2 *)cx2; //Error: derived class "C2" contains more than one instance of class "X"
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment