CPP Object Slicing

Creator
Creator
Seonglae Cho
Created
Created
2022 Oct 7 17:9
Editor
Edited
Edited
2022 Oct 7 17:17
Refs
Refs
struct A { int a; } struct B: A { int a; } void fill (A* z, int n) { for (int i = 0; i < n; i++) z[i].a = i; } main(){ B y[8]; fill(y, 8); cout << y[2].a << endl; } // 4 not 2
because fill function refer A but B is double sized memory
 
 
 
 
 

Recommendations