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