Why is the size of an empty class A {}; 1? If it is not 1, A objects [5] is used to define the array of objects of this class. objects [0] and objects [1] are in the same address and cannot be distinguished.
[Cpp]
# Include <iostream>
Using namespace std;
Class
{
Public:
Virtual void aa (){}
Private:
Char k [3];
};
Class B: public
{
Public:
Virtual void bb (){}
};
Int main ()
{
Cout <"A's size is" <sizeof (A) <endl;
Cout <"B's size is" <sizeof (B) <endl;
Return 0;
}
VS and gcc execution results:
A's size is 8
B's size is 8
Note: A class with virtual functions has a virtual table, which contains all virtual functions of the class. The class has a virtual table pointers, generally, vptr points to the virtual table, which occupies 4 bytes. The member Class B public inherits from Class A. The virtual function table of Class B actually has two virtual functions: A: aa () and B: bb (), the size of Class B is equal to the size of char k [3] and the size of a vptr pointing to the virtual function table pointer. The memory alignment is considered as 8.
[Cpp]
# Include <iostream>
Using namespace std;
Class
{
Public:
Virtual void aa (){}
Private:
Char k [3];
};
Class B: public
{
Public:
// Virtual void bb (){}
};
Int main ()
{
Cout <"A's size is" <sizeof (A) <endl;
Cout <"B's size is" <sizeof (B) <endl;
Return 0;
}
VS and gcc results:
A's size is 8
B's size is 8
Note: Class B does not seem to have A virtual function, but actually it does not have to be rewritten. Because of public inheritance, there is A virtual function A: aa () inherited from (), in fact, the functions in the virtual function tables of Class A and Class B are A: aa ().
[Cpp]
# Include <iostream>
Using namespace std;
Class
{
Public:
Virtual void aa (){}
Virtual void aa2 (){}
Private:
Char k [3];
};
Class B: public
{
Public:
Virtual void bb (){}
Virtual void bb2 (){}
};
Int main ()
{
Cout <"A's size is" <sizeof (A) <endl;
Cout <"B's size is" <sizeof (B) <endl;
Return 0;
}
Vs and gcc
Execution result:
A's size is 8
B's size is 8
Note: if there is a virtual function in a class, no matter how many virtual functions there are, there is only one pointer to the virtual table. Each table item in the virtual table stores the entry address of a virtual function. When calling a virtual function, first find the table items corresponding to the virtual table, find the entry address, and then execute. For direct inheritance, no matter whether there are virtual functions in Class B, because it inherits Class A and Class A contains virtual functions, if Class B has virtual functions, then it is in the same virtual table as class A that belongs to Class B. Note: Private Members in Class A still occupy the memory in Class B.
[Cpp]
# Include <iostream>
Using namespace std;
Class
{
Public:
Virtual void aa (){}
Private:
Char k [3];
};
Class B: virtual public
{
Public:
// Virtual void bb (){}
};
Int main ()
{
Cout <"A's size is" <sizeof (A) <endl;
Cout <"B's size is" <sizeof (B) <endl;
Return 0;
}
Vs and gcc
Execution result: A's size is 8
B's size is 12
Note: Class B contains the inherited char k [3], the inherited virtual function. The virtual function table of Class B contains A: aa (), because it is A virtual inheritance, there is also a Pointer to the parent class, which is a Pointer to the virtual base class (Pointer to virtual base class ). Consider memory alignment. The total size is 12.
[Cpp]
# Include <iostream>
Using namespace std;
Class
{
Public:
Virtual void aa (){}
Private:
Char k [3];
};
Class B: public virtual
{
Public:
Virtual void bb (){}
};
Int main ()
{
Cout <"A's size is" <sizeof (A) <endl;
Cout <"B's size is" <sizeof (B) <endl;
Return 0;
}
VS execution result: A's size is 8
B's size is 16
Gcc execution result: A's size is 8
B's size is 12
Note: For virtual inheritance, Class B must first point to parent class A by adding A pointer, which is called A virtual base class pointer. It then contains three char inherited from the parent class, plus a virtual function pointer. Considering memory alignment, the result in gcc is 4 + 4 + 4 = 12. In VS, the result is 16, why? The difference between this question and the previous question is that a virtual function is added to Class B, but class B has a virtual function table. Debug and view the assembly code in VS, and find that there are no more 4 bytes.
For More information about virtual functions, multi-inheritance, and virtual inheritance, see More objective C ++: understand the costs of virtual functions, multi-inheritance, virtual base classes, and RTTI.