See C + + (virtual function) with Assembly vision

Source: Internet
Author: User

Turn from: http://blog.csdn.net/feixiaoxing/article/details/6779279

Virtual function is an important content in object-oriented design. Its appearance makes us need only the same interface function, and can get different results. But some friends are aware of it, do not know why, why there is such a result, we can use a piece of code to illustrate the problem. First of all, we first define two basic types, one is employee, one is manager, the friends who have seen the previous blog should have a little impression:

View Plain class Employee {Public:employee () {} ~employee () {} virtual void print () const {print   F ("employee!\n");}      }; Class Manager:public Employee {Public:manager () {} ~manager () {} void print () const {printf ("man")   Ager!\n ");}       }; We see that there is a little bit different from the previous member function, where virtual appears before the print function. But it is this virtual that has played a huge role. It is no exaggeration to say that there is no virtual function, basically there is no design pattern, also can not embody the C + + language in the object-oriented design of great superiority. Now let's see how this virtual works.

View Plain 76:       employee p;   0040128d    lea         ecx,[ebp-10h]   00401290    call         @ILT +45 (employee::employee)   (00401032)     00401295   mov         dword ptr [ ebp-4],0   77:       manager m;   0040129C    lea         ecx,[ebp-14h]   0040129f    call         @ILT +65 (manager::manager)   (00401046)    004012a4   mov         byte ptr  [ebp-4],1  

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.