virtual_inheritance.cc 857 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // { dg-do run }
  2. #include <assert.h>
  3. struct V {
  4. V(): virtual_value(-123) {}
  5. int virtual_value;
  6. virtual int access_vv() { return virtual_value; }
  7. };
  8. struct A: virtual public V {
  9. A():value(123) {}
  10. int value;
  11. virtual int access() { return value; }
  12. };
  13. struct B: virtual public V {
  14. B():value(456) {}
  15. int value;
  16. virtual int access() { return value; }
  17. };
  18. struct C : public A, public B {
  19. C():better_value(789) {}
  20. int better_value;
  21. virtual int access() { return better_value; }
  22. };
  23. struct D: public A, public B {
  24. D():better_virtual_value(-345) {}
  25. int better_virtual_value;
  26. virtual int access_vv() { return better_virtual_value; }
  27. };
  28. int use(B *b)
  29. {
  30. return b->access();
  31. }
  32. int v_use(V * v)
  33. {
  34. return v->access_vv();
  35. }
  36. int main()
  37. {
  38. C c;
  39. assert(v_use(&c) == -123);
  40. D d;
  41. assert(v_use(&d) == -345);
  42. return 0;
  43. }