Here, in this code, the size of ob1 is 16 which is fine(because of the virtual pointer) but I can't understand why the size of ob2 is 24.
#include <iostream>
using namespace std;
class A {
int x;
};
class B {
int y, z;
};
class C : virtual public A {
int a;
};
class D : virtual public B {
int b;
};
int main() {
C ob1;
D ob2;
cout << sizeof(ob1) << sizeof(ob2) << "\n";
}
I expect the size of ob2 as 20, but the output is 24
virtual
mechanism requires some overhead. Some compilers place that overhead into the structure. – Zoon'\n'
orstd::endl
. – Zoon