fork download
  1. struct A
  2. {
  3. double x[2];
  4. double i;
  5. };
  6.  
  7. struct B : virtual public A
  8. {
  9. double y[2];
  10. };
  11.  
  12. int main(void)
  13. {
  14. B test;
  15. test.x[0] = 300;
  16. test.i = -5;
  17.  
  18. A const * const pA = &test;
  19. B const * const pB = &test;
  20. bool const b = reinterpret_cast<int const *>(pA)
  21. != reinterpret_cast<int const *>(pB); // As expected
  22.  
  23. double (B::*pmemberBBi) = &B::i;
  24. double (B::*pmemberBBx)[2] = &B::x;
  25. double (A::*pmemberABx)[2] = &B::x;
  26.  
  27. double const * const pBBi_B = &(pB->*pmemberBBi); // Valid pointer even though pointer to derived
  28. double const * const pBB_B = pB->*pmemberBBx ; // Null pointer, but why if the previous line worked?
  29. double const * const pAB_B = pB->*pmemberABx ; // Works (pointer to base)
  30.  
  31. double bbi_b = pB->*pmemberBBi ; // Works fine even though it is pointer to derived
  32. double bb_b = (pB->*pmemberBBx)[0]; // Crash, but why if the previous line worked?
  33. double ab_b = (pB->*pmemberABx)[0]; // Works (pointer to base)
  34.  
  35. return 0;
  36. }
  37.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:23:35: error: pointer to member conversion via virtual base ‘A’
  double (B::*pmemberBBi)    = &B::i;
                                   ^
prog.cpp:24:35: error: pointer to member conversion via virtual base ‘A’
  double (B::*pmemberBBx)[2] = &B::x;
                                   ^
prog.cpp:20:18: warning: unused variable ‘b’ [-Wunused-variable]
  bool      const b  = reinterpret_cast<int const *>(pA)
                  ^
prog.cpp:27:23: warning: unused variable ‘pBBi_B’ [-Wunused-variable]
  double const * const pBBi_B = &(pB->*pmemberBBi); // Valid pointer even though pointer to derived
                       ^
prog.cpp:28:23: warning: unused variable ‘pBB_B’ [-Wunused-variable]
  double const * const pBB_B  = pB->*pmemberBBx   ; // Null pointer, but why if the previous line worked?
                       ^
prog.cpp:29:23: warning: unused variable ‘pAB_B’ [-Wunused-variable]
  double const * const pAB_B  = pB->*pmemberABx   ; // Works (pointer to base)
                       ^
prog.cpp:31:9: warning: unused variable ‘bbi_b’ [-Wunused-variable]
  double bbi_b = pB->*pmemberBBi    ; // Works fine even though it is pointer to derived
         ^
prog.cpp:32:9: warning: unused variable ‘bb_b’ [-Wunused-variable]
  double bb_b  = (pB->*pmemberBBx)[0]; // Crash, but why if the previous line worked?
         ^
prog.cpp:33:9: warning: unused variable ‘ab_b’ [-Wunused-variable]
  double ab_b  = (pB->*pmemberABx)[0]; // Works (pointer to base)
         ^
stdout
Standard output is empty