C ++ (class inheritance) 11 from the perspective of Assembly

Source: Internet
Author: User

[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
  1. ClassEmployee
  2. {
  3. Public:
  4. Employee () {printf ("Employee ()! \ N");}
  5. ~ Employee () {printf ("~ Employee ()! \ N");}
  6. };
  7. ClassManager:PublicEmployee
  8. {
  9. Public:
  10. Manager () {printf ("Manager ()! \ N");}
  11. ~ Manager () {printf ("~ Maneger ()! \ N");}
  12. };

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
  1. 74: manager m;
  2. 00401268 Lea ECx, [ebp-4]
  3. 0040126b call @ ILT + 60 (Manager: Manager) (00401041)
  4. 75 :}
  5. 00401270 Lea ECx, [ebp-4]
  6. 00401273 call @ ILT + 0 (Manager ::~ Manager) (00401005)
  7. 00401278 pop EDI
  8. 00401279 pop ESI
  9. 004020.a pop EBX
  10. 004010000b add ESP, 44 h
  11. 0040da-e cmp ebp, ESP
  12. 00401280 call _ chkesp (00408760)
  13. 00401285 mov ESP, EBP
  14. 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
  1. 65:ClassManager:PublicEmployee
  2. 66 :{
  3. 67:Public:
  4. 68: Manager () {printf ("Manager ()! \ N");}
  5. 004012a0 push EBP
  6. 004012a1 mov EBP, ESP
  7. 004012a3 sub ESP, 44 h
  8. 004012a6 push EBX
  9. 004012a7 push ESI
  10. 004012a8 push EDI
  11. 004012a9 push ECx
  12. 004012aa Lea EDI, [ebp-44h]
  13. 004012ad mov ECx, 11 h
  14. 004012b2 mov eax, 0 cccccccch
  15. 004012b7 rep STOs dword ptr [EDI]
  16. 004012b9 pop ECx
  17. 004012ba mov dword ptr [ebp-4], ECx
  18. 004012bd mov ECx, dword ptr [ebp-4]
  19. 004012c0 call @ ILT + 40 (employee: Employee) (0040102d)
  20. 004012c5 push offset string"Manager ()! \ N"(00431020)
  21. 004012ca call printf (004086e0)
  22. 004012cf add ESP, 4
  23. 004012d2 mov eax, dword ptr [ebp-4]
  24. 004012d5 pop EDI
  25. 004012d6 pop ESI
  26. 004012d7 pop EBX
  27. 004012d8 add ESP, 44 h
  28. 004012db cmp ebp, ESP
  29. 004012dd call _ chkesp (00408760)
  30. 004012e2 mov ESP, EBP
  31. 004012e4 pop EBP
  32. 004012e5 RET

We found that the default constructor of employee is added to the Manager constructor. What about the Destructor?

[CPP] View plaincopy
  1. 69 :~ Manager () {printf ("~ Maneger ()! \ N");}
  2. 00401350 push EBP
  3. 00401351 mov EBP, ESP
  4. 00401353 sub ESP, 44 h
  5. 00401356 push EBX
  6. 00401357 push ESI
  7. 00401358 push EDI
  8. 00401359 push ECx
  9. 0040135a Lea EDI, [ebp-44h]
  10. 0040135d mov ECx, 11 h
  11. 00401362 mov eax, 0 cccccccch
  12. 00401367 rep STOs dword ptr [EDI]
  13. 00401369 pop ECx
  14. 0040136a mov dword ptr [ebp-4], ECx
  15. 0040136d push offset string"~ Maneger ()! \ N"(00431040)
  16. 00401372 call printf (004086e0)
  17. 00401377 add ESP, 4
  18. 0040137a mov ECx, dword ptr [ebp-4]
  19. 0040137d call @ ILT + 5 (employee ::~ Employee) (0040100a)
  20. 00401382 pop EDI
  21. 00401383 pop ESI
  22. 00401384 pop EBX
  23. 00401385 add ESP, 44 h
  24. 00401388 cmp ebp, ESP
  25. 0040138a call _ chkesp (00408760)
  26. 0040138f mov ESP, EBP
  27. 00401391 pop EBP
  28. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.