Consider the code below:
#...
class A {};
class B: public A{};
class C: virtual public A{};
class D: virtual public C{};
// No More Classes
...
int _tmain(int argc, _TCHAR* argv[]) {
cout<<sizeof(A)<<" ";
cout<<sizeof(B)<<" ";
cout<<sizeof(C)<<" ";
cout<<sizeof(D)<<".";
...
}
O/P: 1 1 4 8.
Question:
*. its' my 1st question here, please correct me if you found anything wrong.
In short it's not due to the class being virtual or not, it's because the standard requires that all objects be distinguishable by their memory address. See this question:
Why is the size of an empty class in C++ not zero?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With