Define function in C ++

xiaoxiao2021-03-06  40

Today, in order to deepen your own understanding of the virtual function, I did a fun small test. Define class A, class B and class C inherit the class A class B. code show as below:

#include

#include

Class A

{

PUBLIC:

A () {}

Virtual void infoa () {std :: cout << "A :: infoa ()" << std :: endl;}

Private:

Char arraya [3];

}

Class B

{

PUBLIC:

B () {}

Virtual void inofb () {std :: cout << "b :: infob ()" << std :: end1;

Private:

CHAR ARRAYB [3];

}

Class C: Public A, PUBLIC B

{

PUBLIC:

C () {}

Virtual void infoa () {std :: cout << "C :: infoa ()" << std :: end1;

Virtual void infob () {std :: cout << "C :: inofb ()" << std :: endl;}

Private:

Char arrayc [3];

}

int main ()

{

A a a;

B b;

C C;

Std :: cout << "SIZEOF (A) =:" << SizeOf (a) << std :: end1

Std :: cout << "SIZEOF (B) =:" << sizeof (b) << std :: end1

Std :: cout << "SIZEOF (C) =:" << sizeof (c) << std :: endl;

Return 0;

}

In my computer, the output is as follows:

Sizeof (a) =: 8

SizeOf (b) =: 8

SizeOf (C) =: 20

I just felt a bit strange, why is the class A account for 8 bytes? Because the virtual function is maintained by a pointer called a virtual function allocation table (_VFPTR), the pointer occupies 4 bytes in the 32-bit machine. I think something is a bit, but why is it 8 bytes instead of 7? Look at the defined class A, define a virtual function, and define a private char array, assign 3 bytes of space. If I just said there is a virtual function allocation table pointer, then add 3 should be 7 One byte is right. Later, I thought I realized that the address used in the computer memory will automatically align. Because I store 3 bytes of data, the computer is 32-bit, because the reason it is aligned, it actually allocates 4 bytes, but only 3 you are available. If this is the last result, the size of SIZEOF (A) is 8 is not surprising. Then the same sizeOf (b) is also 8 bytes. There is also a problem, why is SIZEOF (C) that are 20 bytes? Later, I saw that I would like to see the assembly code. I know that the original class C has a copy of Class A, B, and there is a 3-byte array, which is also because of alignment, so 8 8 4 = 20 words. Section. Illustration: ____________ | class a || __________ || ______________________________ | | [0] Void A :: Infoa () || Arraya [0] | | __________________ || Arraya [1] | | __________ | memory alignment ___________________________________________________ | | [0] Void B :: INFOB () || Arrayb [0 ] | | __________________ || arrayb [1] | | arrayb [2] | | __________ | memory alignment

___________________ || A || __________ || _vfptr | -----> ________________________________ | | [0] Void A :: Infoa () || Arraya [0] | | _________________ || Arraya [1] | | ARRAYA [2] || memory alignment || __________ || b || __________ || _VFPTR | -----> _____________________ | | [0] Void B :: Infob () || Arrayb [0] | | | || Arrayb [1] || arrayb [2] || memory alignment || ___________ || arrayc [0] || arrayc [1] || arrayc [2] || memory ie || __________________________________ Here I will leave a question. Now it is the case where the class is inherited, then what is the situation of the Template class inheritance function? Waiting for two days, I have a time to do an experiment. . . to be continued. . .

转载请注明原文地址:https://www.9cbs.com/read-85866.html

New Post(0)