fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct A
  6. {
  7. int x;
  8. A(int x) : x(x) {}
  9. };
  10.  
  11. struct B : A
  12. {
  13. B() : A(7) {}
  14. // int y; // if uncomment, program vouldn't compile
  15. };
  16.  
  17. static_assert(sizeof (A) == sizeof (B), "B must have same size as A");
  18.  
  19. int main()
  20. {
  21. A *a = new B[4];
  22.  
  23. for (size_t q=0; q<4; ++q)
  24. cout << q << ": " << a[q].x << endl;
  25.  
  26. delete [] a;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
0: 7
1: 7
2: 7
3: 7