C++的多态如何在编译和运行期实现


多态是什么?简单来说,就是某段程序调用了一个API接口,但是这个API有许多种实现,根据上下文的不同,调用这段API的程序,会调用该API的不同实现。今天我们只关注继承关系下的多态。

还是得通过一个例子来看看C++是怎样在编译期和运行期来实现多态的。很简单,定义了一个Father类,它有一个testVFunc虚函数哟。再定义了一个继承Father的Child类,它重新实现了testVFunc函数,当然,它也学习Father定义了普通的成员函数testFunc。大家猜猜程序的输出是什么?

[cpp]
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class Father  
  5. {  
  6. public:  
  7.     int m_fMember;  
  8.   
  9.     void testFunc(){  
  10.         cout<<"Father testFunc "<<m_fMember<<endl;  
  11.     }  
  12.     virtual void testVFunc(){  
  13.         cout<<"Father testVFunc "<<m_fMember<<endl;  
  14.     }  
  15.     Father(){m_fMember=1;}  
  16. };  
  17.   
  18. class Child : public Father{  
  19. public:  
  20.     int m_cMember;  
  21.     Child(){m_cMember=2;}  
  22.       
  23.     virtual void testVFunc(){cout<<"Child testVFunc "<<m_cMember<<":"<<m_fMember<<endl;}  
  24.     void testFunc(){cout<<"Child testFunc "<<m_cMember<<":"<<m_fMember<<endl;}  
  25.     void testNFunc(){cout<<"Child testNFunc "<<m_cMember<<":"<<m_fMember<<endl;}  
  26. };  
  27.   
  28.   
  29. int main()  
  30. {  
  31.     Father* pRealFather = new Father();  
  32.     Child* pFalseChild = (Child*)pRealFather;  
  33.     Father* pFalseFather = new Child();  
  34.       
  35.     pFalseFather->testFunc();  
  36.     pFalseFather->testVFunc();  
  37.   
  38.     pFalseChild->testFunc();  
  39.     pFalseChild->testVFunc();      
  40.     pFalseChild->testNFunc();      
  41.   
  42.     return 0;  
  43. }  
同样调用了testFunc和testVfunc,输出截然不同,这就是多态了。它的g++编译器输出结果是:

[cpp]
  1. Father testFunc 1  
  2. Child testVFunc 2:1  
  3. Child testFunc 0:1  
  4. Father testVFunc 1  
  5. Child testNFunc 0:1  
看看main函数里调用的五个test*Func方法吧,这里有静态的多态,也有动态的多态。编译是静态的,运行是动态的。以下解释C++编译器是怎么形成上述结果的。


首先让我们用gcc -S来生成汇编代码,看看main函数里是怎么调用这五个test*Func方法的。

[cpp]
  1.         movl    $16, %edi  
  2.         call    _Znwm   
  3.         movq    %rax, %rbx  
  4.         movq    %rbx, %rdi  
  5.         call    _ZN6FatherC1Ev  
  6.         movq    %rbx, -32(%rbp)  
  7.         movq    -32(%rbp), %rax  
  8.         movq    %rax, -24(%rbp)  
  9.         movl    $16, %edi  
  10.         call    _Znwm   
  11.         movq    %rax, %rbx  
  12.         movq    %rbx, %rdi  
  13.         call    _ZN5ChildC1Ev  
  14.         movq    %rbx, -16(%rbp)  
  15.         movq    -16(%rbp), %rdi  
  16. <span style="color:#ff0000;">        call    _ZN6Father8testFuncEv    本行对应pFalseFather->testFunc();</span>  
  17.         movq    -16(%rbp), %rax  
  18.         movq    (%rax), %rax  
  19.         movq    (%rax), %rax  
  20.         movq    -16(%rbp), %rdi  
  21. <span style="color:#ff0000;">        call    *%rax                                        本行对应pFalseFather->testVFunc();</span>  
  22.         movq    -24(%rbp), %rdi  
  23. <span style="color:#ff0000;">        call    _ZN5Child8testFuncEv     本行对应pFalseChild->testFunc();</span>  
  24.         movq    -24(%rbp), %rax  
  25.         movq    (%rax), %rax  
  26.         movq    (%rax), %rax  
  27.         movq    -24(%rbp), %rdi  
  28. <span style="color:#ff0000;">        call    *%rax                                        本行对应pFalseChild->testVFunc();    </span>  
  29.         movq    -24(%rbp), %rdi  
  30. <span style="color:#ff0000;">        call    _ZN5Child9testNFuncEv        本行对应pFalseChild->testNFunc();    </span>  
  31.         movl    $0, %eax  
  32.         addq    $40, %rsp  
  33.         popq    %rbx  
  34.         leave  

红色的代码,就是在依次调用上面5个test*Func。可以看到,第1、3次testFunc调用,其结果已经在编译出来的汇编语言中定死了,C++代码都是调用某个对象指针指向的testFunc()函数,输出结果却不同,第1次是:Father testFunc 1,第3次是:Child testFunc 0:1,原因何在?在编译出的汇编语言很明显,第一次调用的是_ZN6Father8testFuncEv代码段,第三次调用的是_ZN5Child8testFuncEv代码段,两个不同的代码段!编译完就已经决定出同一个API用哪种实现,这就是编译期的多态。

  • 1
  • 2
  • 下一页

相关内容