C++虚函数调用的反汇编解析

论坛 期权论坛     
选择匿名的用户   2021-6-2 17:54   251   0
<p align="center" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center"><strong><span style="FONT-SIZE: 14pt; mso-bidi-font-size: 12.0pt">C&#43;&#43;</span></strong><strong><span lang="ZH-CN" style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">虚函数调用的反汇编解析</span></strong><strong><span style="FONT-SIZE: 14pt; mso-bidi-font-size: 12.0pt">
   </span></strong></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="mso-spacerun: yes">    </span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
</p>
<p align="center" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center"><strong><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">作者:阮建辉</span>
  </strong></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
</p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 11.9pt"><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">虚函数的调用如何能实现其“虚”?作为</span>C&#43;&#43;<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">多态的表现手段,估计很多人对其实现机制感兴趣。大约一般的教科书就说到这个</span>C&#43;&#43;<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">强大机制的时候,就是教大家怎么用,何时用,而不会去探究一下这个虚函数的真正实现细节。(当然,因为不同的编译器厂家,可能对虚函数有自己的实现,呵呵,这就算是虚函数对于编译器的“多态”了:)。</span><span lang="ZH-CN"> </span><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">作为编译型语言,</span>C&#43;&#43;<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">编译的最后结果就是一堆汇编指令了(这里不同于</span>.NET<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">的</span>CLR<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">)。今天,我就来揭开它的神秘面纱,从汇编的层面来看看虚函数到底怎么实现的。让大家对虚函数的实现不仅知其然,更知其所以然。<strong>(本文程序环境为:</strong></span><strong>PC &#43; Windows XP Pro &#43; Visual C&#43;&#43;6.0</strong><strong><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">,文中所得出来的结果和反映的编译器策略也只针对</span>VC6.0</strong><strong><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">的编译器)</span></strong></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 12pt">
</p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 12pt"><span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: &#39;Times New Roman&#39;; mso-hansi-font-family: &#39;Times New Roman&#39;">先看一段简单代码:</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 12pt; tab-stops: 0cm"><strong><em><u><span style="COLOR: red">Code Segment:
     </span></u></em></strong></p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 0.5pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 0.5pt solid">
<p class="MsoNormal" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; MARGIN: 0cm 0cm 0pt; BORDER-LEFT: medium none; TEXT-INDENT: 12pt; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 0cm"><span style="COLOR: silver">Line01:</span><span style="mso-spacerun: yes">  </span>#include &lt;stdio.h&gt;</p>
<p class="MsoNormal" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; MARGIN: 0cm 0cm 0pt; BORDER-LEFT: medium none; TEXT-INDENT: 12pt; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 0cm"><span style="COLOR: silver">Line02:
   </span></p>
<p class="MsoNormal" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; MARGIN: 0cm 0cm 0pt; BORDER-LEFT: medium none; TEXT-INDENT: 12pt; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 0cm"><span style="COLO
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP