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:
- sizeof(A) = 1byte, and this location hold what significant for compiler/us.
- Why compiler bother to add vptr in C class object when there is nothing actually resides.
- If we are not having any virtual function, compiler is adding an extra vptr to derived objects.
*. its' my 1st question here, please correct me if you found anything wrong.