First look at an example:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class a{};
Class B
{
int b;
char c;
};
Class C
{
int C1;
static int C2;
};
int c::c2 = 1;
Class D:public C,public b{
int D;
};
int main ()
{
cout<< "sizeof (a) =" <<sizeof (a) <<endl;
cout<< "sizeof (b) =" <<sizeof (b) <<endl;
cout<< "sizeof (c) =" <<sizeof (c) <<endl;
cout<< "sizeof (d) =" <<sizeof (d) <<endl;
return 0;
}
the results of the operation are:
sizeof (A) =1
sizeof (B) =8
sizeof (C) =4
sizeof (D) =16
For Class A, although a is an empty class, but in order to facilitate the instantiation of an empty class, the compiler often assigns it a byte, which gives it a unique address in memory after the instantiation of a. The size of the class b,b should be sizeof (int) +sizeof (char) = 5, However, considering memory alignment, the size of B should be 8. For Class C, the static member variables of the class are placed in the global area, and the normal members of the class are not placed together. A static member of a class is declared, but a non-static member exists only if the class is instantiated. So the size of C is sizeof (int) = 4. The size of D is the size of the B+c + the size of its own data members, altogether 16.
========================== Split line is here ====================================
The size of the class that contains the virtual function is discussed below:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class A
{
Public
void Virtual AA () {};
};
Class B:public A
{
void Virtual bb () {};
};
Class C:virtual A
{
Public
void Virtual AA () {};
void cc () {};
};
Class D:virtual A
{
Public
void virtual DD () {};
};
int main ()
{
cout<< "sizeof (a) =" <<sizeof (a) <<endl;
cout<< "sizeof (b) =" <<sizeof (b) <<endl;
cout<< "sizeof (c) =" <<sizeof (c) <<endl;
cout<< "sizeof (d) =" <<sizeof (d) <<endl;
return 0;
}
the results of the operation are:
sizeof (A) =4
sizeof (B) =4
sizeof (C) =8
sizeof (D) =12
For Class A, it contains a virtual function, the compiler will generate a virtual function table for the virtual function, to record the corresponding function address, for this, in Class A memory address to have a vfptr_a pointer to the virtual table, so Class A is the size of the pointer size, That is 4. (Note that regardless of the number of virtual functions in the class, their size is 4, because you only need to save this pointer in memory).
For Class B, it is public inheritance a, although it also has a virtual function, but from the result, B should and a are in B's vtable (virtual table), so Class B has a size of 4.
For class C, it is vitual inherit a, so there is a pointer to the parent class A, 4 byte size AA () is inherited from Class A virtual function, from the result, it does not occupy space in memory, so the size of C is sizeof (A) +4=8.
For Class D, it is a virtual inheritance Class A, ditto, to have a pointer to the parent class A, while Class D has a virtual function, so there is a pointer to the virtual table, so sizeof (D) =sizeof (a) +4+4=12