The following code is said in the book about memory alignment after inheritance:
1#include <bits/stdc++.h>2 using namespacestd;3 classA4 {5 Private:6 intVal;7 Charbit1;8 };9 classD | PublicATen { One Private: A CharBit2; - }; - classE | PublicB the { - Private: - Charbit3; - }; + intMain () - { +cout <<sizeof(A) << Endl <<sizeof(B) << Endl <<sizeof(C) <<Endl; A return 0; at}
And said the output should be 8 12 16, and explained the reason for doing so, I thought that reason is very strange, or did not understand the reason in the book is what exactly, so tested, found that the GCC compiler output results of 8 8 8, it is true that bit2,bit3 and bit1 bound together, At least the statement in the book is wrong on the current GCC compiler.
Explore the C + + object model in depth reading notes: Data semantics-memory alignment issues in inheritance and data member