When the beginner is studying the object-oriented programming language, more or less have some questions, the code we wrote and the final student code is large, we don't know what the compiler has done in the background. These are because we stay only in the language layer, the so-called language layer is the church of our basic grammar rules, but will not tell us why do you do this? Today, a little feeling is that I am in learning programming, a specific function in the compiler.
First of all: We have to know what is an instantiation of classes. The so-called instantiation is allocated in memory.
Then let's take a look at an example:
#include
Class A {}; class b {}; class c: public a {Virtual void fun () = 0;}; Class D: public b, public c {}; 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 output result executed by the program is:
SizeOf (a) = 1
SizeOf (b) = 1
Sizeof (c) = 4
Sizeof (d) = 8
Why do this result? Is the beginner will definitely worry? Class A, B is clearly empty, its size should be 0, why is the result of the compiler output? This is the reason why the instantiation we just said (empty class can also be instantiated), each instance has a unique address in memory, in order to achieve this, the compiler often gives an empty class implied Add a byte so that empty class has a unique address after instantification. Therefore, the size of A and B is 1.
The class c is derived from class A, which has a pure virtual function. Due to the virtual function, there is a pointer (VPTR) pointing to the virtual function, and the size of the 32-bit system is assigned to the pointer is 4. Bytes, so finally get the size of Class Class 4.
The size of the class D is more confused, and the class D is derived from class B, C, and its size should be the sum of two, why is it 8? This is because the access efficiency of the instance is increasing in memory. The size of the class is often adjusted to the system's integer times. And take the law, which is the most fact, which is the size of this class, so the size of class D is 8 bytes.
Of course, the results obtained on different compilers may differ, but this experiment tells us that beginners, no matter whether they are empty, they can be instantiated (empty class can also be instantiated), each of which has one Unique address.
The compiler I used is VC 6.0.
Let's take another example.
#include
Class b {private: int data; static int data1;}; int b :: data1 = 0; void mian () {cout << "SIZEOF (a) = << SizeOf (a) << endl; cout << "SIZEOF (B) =" << sizeof (b) << Endl;} Executive result is:
SizeOf (a) = 4;
SizeOf (b) = 4;
Why is the class B a data member, but the size and the size of class A are the same size? Because: The static data member of class B is placed in a Global Data Member in the program, which is a data member of the class. However, it does not affect the size of the class, no matter how many instances do this class, or how many new classes are derived, static member data will always only have an entity in the class, and the category of non-static data members are only instantiated. They only exist. However, once the static data member of the class is declared, it already exists regardless of whether the class is instantiated. It can be said that static data members of the class are a special global variable.
Therefore, the size of A and B is the same.
Let's take a look at the size of a constructor, and the destructive function. What is it?
#include
Private: int x; int g;}; class b {public: private: int data; int data2; static int xs;}; int b :: xs = 0; void main () {a s (10); sf ( 10); COUT << "Sozeof (a) << sizeof (a) << endl; cout <<" SIZEOF (B) "<< sizeof (b) << endl;} program execution output result is:
10,
Sizeof (a) 8
SIZEOF (B) 8
The same results can be seen that the size of the class is independent of the constructor, the destructive function, and other member functions in it, only related to member data among it.
Several examples from the above are not difficult to find the size of the class:
1. The sum of the types of non-static member data for class.
2. There is a size variable of the compiler to support certain features (such as pointers pointing to virtual functions).
3. In order to optimize access efficiency, the edge adjustment is performed.
4 Nor related to constructor, destructor, and other member functions in the class.