Consider the code below:
#...
class A {};
class B: public A{};
class C: virtual public A{};
class D: virtual public C{};
...
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:
- sizeof (A) = 1 byte, and this place contains what is important for the compiler / us.
- Why does the compiler want to add vptr to an object of class C when nothing exists.
- If we do not have a virtual function, the compiler adds additional vptr to the derived objects.
*. its my 1st question here, please correct me if you find something is wrong.
source
share