fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct A {
  6. int a;
  7. };
  8.  
  9. struct B {
  10. int b;
  11. };
  12.  
  13. struct C: A, B {
  14. int c;
  15. };
  16.  
  17.  
  18. struct Base {
  19. virtual B* func() = 0;
  20. virtual ~Base() = default;
  21. };
  22.  
  23. struct Derived: Base {
  24. C* func() {
  25. auto p = new C;
  26. cout << p << endl;
  27. return p;
  28. }
  29. };
  30.  
  31.  
  32. int main() {
  33. Base* pb = new Derived;
  34. cout << pb->func() << endl;
  35. // the outputed two addresses are different.
  36. // how is the pointer cast (adding some offset) achieved?
  37. }
  38.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
0x8b44a20
0x8b44a24