View C ++ from the perspective of assembly (class inheritance)

Source: Internet
Author: User
Tags class manager

 

[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 code?

 

 

Copy to clipboardprint? Class employee

{

Public:

Employee () {printf ("employee ()! \ N ");}

~ Employee () {printf ("~ Employee ()! \ N ");}

};

 

Class manager: public employee

{

Public:

Manager () {printf ("manager ()! \ N ");}

~ Manager () {printf ("~ Maneger ()! \ N ");}

};

Class employee

{

Public:

Employee () {printf ("employee ()! \ N ");}

~ Employee () {printf ("~ Employee ()! \ N ");}

};

 

Class manager: public employee

{

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?

 

 

Copy to clipboardprint? 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

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?

 

 

Copy to clipboardprint? 65: class manager: public employee

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

65: class manager: public employee

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?

 

 

Copy to clipboardprint? 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

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.

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.