In the following code:
#include <iostream>
class a {
char a;
};
class b : virtual public a {
char b;
};
class c : virtual public a {
char c;
};
class d : public b, public c {
char d;
};
int main() {
std::cout << "sizeof a: " << sizeof(a) << std::endl;
std::cout << "sizeof b: " << sizeof(b) << std::endl;
std::cout << "sizeof c: " << sizeof(c) << std::endl;
std::cout << "sizeof d: " << sizeof(d) << std::endl;
return 0;
}
the output is:
sizeof a: 1
sizeof b: 16
sizeof c: 16
sizeof d: 32
I want to know why the increased size is 16. size of char is 1 that means increased size is 15. I know that virtual class needs a pointer for its offset that adds 4 bytes then 11 bytes does not make any sense. Can anyone explain why does it happen, my question is different as it is a diamond inheritance case