fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct A {
  6. int a[64];
  7. A() { cout << "A()" << endl; }
  8. A(const A&) { cout << "A(A&)" << endl; }
  9. A& operator = (const A&) { cout << "A=A" << endl; return *this; }
  10. };
  11.  
  12. struct B : virtual public A {
  13. int b[64];
  14. B() { cout << "B()" << endl; }
  15. B(const B&) { cout << "B(B&)" << endl; }
  16. B(const A&) { cout << "B(A&)" << endl; }
  17. B& operator = (const B&) { cout << "B=B" << endl; return *this; }
  18. B& operator = (const A&) { cout << "B=A" << endl; return *this; }
  19. };
  20.  
  21. struct C : virtual public A {
  22. int c[64];
  23. C() { cout << "C()" << endl; }
  24. C(const C&) { cout << "C(C&)" << endl; }
  25. C(const B&) { cout << "C(B&)" << endl; }
  26. C(const A&) { cout << "C(A&)" << endl; }
  27. C& operator = (const C&) { cout << "C=C" << endl; return *this; }
  28. C& operator = (const B&) { cout << "C=B" << endl; return *this; }
  29. C& operator = (const A&) { cout << "C=A" << endl; return *this; }
  30. };
  31.  
  32. struct END : virtual public B, C {
  33. int end[64];
  34. END() { cout << "END()" << endl; }
  35. END(const END&) { cout << "END(END&)" << endl; }
  36. END(const C&) { cout << "END(C&)" << endl; }
  37. END(const B&) { cout << "END(B&)" << endl; }
  38. END(const A&) { cout << "END(A&)" << endl; }
  39. END& operator = (const END&) { cout << "END=END" << endl; return *this; }
  40. END& operator = (const C&) { cout << "END=C" << endl; return *this; }
  41. END& operator = (const B&) { cout << "END=B" << endl; return *this; }
  42. END& operator = (const A&) { cout << "END=A" << endl; return *this; }
  43. };
  44.  
  45. int main() {
  46. END* end = new END();
  47.  
  48. A *a = dynamic_cast<A*>(end);
  49. B *b = dynamic_cast<B*>(end);
  50. C *c = dynamic_cast<C*>(end);
  51.  
  52. std::cout << "end = " << (void*)end << std::endl;
  53. std::cout << "a = " << (void*)a << std::endl;
  54. std::cout << "b = " << (void*)b << std::endl;
  55. std::cout << "c = " << (void*)c << std::endl;
  56.  
  57. // the direct pointers are going to have to differ
  58. // to point to the correct vtable. what about 'a' in all cases?
  59. std::cout << "end->a = " << (void*)&(end->a) << std::endl;
  60. std::cout << "a->a = " << (void*)&(a->a) << std::endl;
  61. std::cout << "b->a = " << (void*)&(b->a) << std::endl;
  62. std::cout << "c->a = " << (void*)&(c->a) << std::endl;
  63.  
  64.  
  65. }
  66.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
A()
B()
C()
END()
end = 0x823a008
a = 0x823a310
b = 0x823a20c
c = 0x823a008
end->a = 0x823a310
a->a = 0x823a310
b->a = 0x823a310
c->a = 0x823a310