// This code runs successfully in 32-bit win
# Include <iostream>
Using namespace std;
Class A // A is an empty class. The compiler will mark this class with A char type and the size is 1.
{
};
Class B: public A // B inherits A, but it is also an empty class with the size of 1
{
};
Class C: virtual public A // during virtual inheritance, the compiler inserts A pointer pointing to the parent class with A size of 4
{
};
Class D // The size is 4
{
Public:
Int;
Static int B; // static variables are stored in the static storage area.
};
Class E // print function does not occupy memory space, size is 4
{
Public:
Void print () {cout <"E" <endl ;}
Private:
Int;
};
Class F // virtual functions occupy the memory of a pointer. The system needs to use this pointer to maintain the virtual function table. The size is 8.
{
Public:
Virtual void print () {cout <"F" <endl ;}
Private:
Int;
};
Class G: public F // The memory size of one more virtual function remains unchanged. It can be seen that a class has only one virtual function pointer. The size is 8.
{
Public:
Virtual void print () {cout <"G" <endl ;}
Virtual void print2 () {cout <"G2" <endl ;}
};
Int main ()
{
A;
B B;
C c;
D;
E e;
F f;
G g;
Cout <sizeof (A) <"" <sizeof (a) <endl; // 1
Cout <sizeof (B) <"" <sizeof (B) <endl; // 1
Cout <sizeof (C) <"" <sizeof (c) <endl; // 4 4
Cout <sizeof (D) <"" <sizeof (d) <endl; // 4 4
Cout <sizeof (E) <"" <sizeof (e) <endl; // 4 4
Cout <sizeof (F) <"" <sizeof (f) <endl; // 8
Cout <sizeof (G) <"" <sizeof (g) <endl; // 8
Return 0;
}
From C Xiaojia