[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Inheritance is a basic attribute of a class, but how is the function initialized during the class inheritance process? How can we analyze the structure? Let's take a look at the following section.Code?
[CPP] View plaincopy
-
- ClassEmployee
-
- {
-
- Public:
-
- Employee () {printf ("Employee ()! \ N");}
-
- ~ Employee () {printf ("~ Employee ()! \ N");}
-
- };
-
- ClassManager:PublicEmployee
-
- {
-
- Public:
-
- Manager () {printf ("Manager ()! \ N");}
-
- ~ Manager () {printf ("~ Maneger ()! \ N");}
-
- };
Seeing the code above, I believe everyone understands that we have defined such a class. The base class is empoyee, and the inheritance class is Manager. We can see that manager is a special kind of employee. How can we arrange functions during memory construction and analysis?
[CPP] View plaincopy
-
- 74: manager m;
-
- 00401268 Lea ECx, [ebp-4]
-
- 0040126b call @ ILT + 60 (Manager: Manager) (00401041)
-
- 75 :}
-
- 00401270 Lea ECx, [ebp-4]
-
- 00401273 call @ ILT + 0 (Manager ::~ Manager) (00401005)
- 00401278 pop EDI
-
- 00401279 pop ESI
-
- 004020.a pop EBX
-
- 004010000b add ESP, 44 h
-
- 0040da-e cmp ebp, ESP
-
- 00401280 call _ chkesp (00408760)
- 00401285 mov ESP, EBP
-
- 00401287 pop EBP
We found that the manager structure and analysis structure are also simple. A constructor is constructed when a variable appears. When will the structure be analyzed? The function is analyzed at the end of the process. Next, we can further discuss how the structure and structure of the Manager are actually implemented?
[CPP] View plaincopy
-
- 65:ClassManager:PublicEmployee
-
- 66 :{
-
- 67:Public:
- 68: Manager () {printf ("Manager ()! \ N");}
-
- 004012a0 push EBP
-
- 004012a1 mov EBP, ESP
-
- 004012a3 sub ESP, 44 h
-
- 004012a6 push EBX
-
- 004012a7 push ESI
-
- 004012a8 push EDI
- 004012a9 push ECx
-
- 004012aa Lea EDI, [ebp-44h]
-
- 004012ad mov ECx, 11 h
-
- 004012b2 mov eax, 0 cccccccch
-
- 004012b7 rep STOs dword ptr [EDI]
-
- 004012b9 pop ECx
- 004012ba mov dword ptr [ebp-4], ECx
-
- 004012bd mov ECx, dword ptr [ebp-4]
-
- 004012c0 call @ ILT + 40 (employee: Employee) (0040102d)
-
- 004012c5 push offset string"Manager ()! \ N"(00431020)
-
- 004012ca call printf (004086e0)
- 004012cf add ESP, 4
-
- 004012d2 mov eax, dword ptr [ebp-4]
-
- 004012d5 pop EDI
-
- 004012d6 pop ESI
-
- 004012d7 pop EBX
-
- 004012d8 add ESP, 44 h
- 004012db cmp ebp, ESP
-
- 004012dd call _ chkesp (00408760)
-
- 004012e2 mov ESP, EBP
-
- 004012e4 pop EBP
-
- 004012e5 RET
We found that the default constructor of employee is added to the Manager constructor. What about the Destructor?
[CPP] View plaincopy
- 69 :~ Manager () {printf ("~ Maneger ()! \ N");}
-
- 00401350 push EBP
-
- 00401351 mov EBP, ESP
-
- 00401353 sub ESP, 44 h
-
- 00401356 push EBX
-
- 00401357 push ESI
- 00401358 push EDI
-
- 00401359 push ECx
-
- 0040135a Lea EDI, [ebp-44h]
-
- 0040135d mov ECx, 11 h
-
- 00401362 mov eax, 0 cccccccch
-
- 00401367 rep STOs dword ptr [EDI]
-
- 00401369 pop ECx
- 0040136a mov dword ptr [ebp-4], ECx
-
- 0040136d push offset string"~ Maneger ()! \ N"(00431040)
-
- 00401372 call printf (004086e0)
-
- 00401377 add ESP, 4
-
- 0040137a mov ECx, dword ptr [ebp-4]
- 0040137d call @ ILT + 5 (employee ::~ Employee) (0040100a)
-
- 00401382 pop EDI
-
- 00401383 pop ESI
-
- 00401384 pop EBX
-
- 00401385 add ESP, 44 h
-
- 00401388 cmp ebp, ESP
- 0040138a call _ chkesp (00408760)
-
- 0040138f mov ESP, EBP
-
- 00401391 pop EBP
-
- 00401392 RET
We found that when the manager is constructed, the employee is constructed first, and then the manager is printed. When the structure is analyzed, the manager first analyzes itself and then calls the destructor of the employee, the assembly code above demonstrates everything.