fork download
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5. int x;
  6. A() : x(1) {}
  7. A(int X) : x(X) {}
  8. void DoStuff(){ std::cout<<"A::DoStuff"<<std::endl; }
  9. virtual void Go(){ std::cout<<"A::Go"<<std::endl; }
  10. virtual ~A(){}
  11. };
  12. struct B
  13. {
  14. int x;
  15. B() : x(2) {}
  16. void DoStuff(){ std::cout<<"B::DoStuff"<<std::endl; }
  17. virtual void Go(){ std::cout<<"B::Go"<<std::endl; }
  18. virtual ~B(){}
  19. };
  20.  
  21. struct C : A, B
  22. {
  23. virtual void Go(){ std::cout<<"C::Go"<<std::endl; }
  24. virtual ~C(){}
  25. };
  26. struct D : A, B
  27. {
  28. D() : A(4) {}
  29. using A::Go;
  30. virtual ~D(){}
  31. };
  32.  
  33. int main()
  34. {
  35. using namespace std;
  36.  
  37. C c;
  38. // c.DoStuff(); //error
  39. c.A::DoStuff();
  40. c.B::DoStuff();
  41. c.Go();
  42. c.A::Go();
  43. c.B::Go();
  44. // cout << c.x << endl; //error
  45. cout << c.A::x << endl;
  46. cout << c.B::x << endl;
  47.  
  48. cout << endl;
  49.  
  50. D d;
  51. // d.DoStuff(); //error
  52. d.A::DoStuff();
  53. d.B::DoStuff();
  54. d.Go();
  55. d.A::Go();
  56. d.B::Go();
  57. // cout << d.x << endl; //error
  58. cout << d.A::x << endl;
  59. cout << d.B::x << endl;
  60. }
Success #stdin #stdout 0.01s 2728KB
stdin
Standard input is empty
stdout
A::DoStuff
B::DoStuff
C::Go
A::Go
B::Go
1
2

A::DoStuff
B::DoStuff
A::Go
A::Go
B::Go
4
2