⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 [c++对象模型][8]多重继承与虚函数表 - itech's blog - 博客园.htm

📁 最为全面的c++内存模型探秘。不管是初学者还是老手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;&nbsp;}<BR>};<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;D&nbsp;:&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;B1,&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;B2,&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;B3<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN 
style="COLOR: #000000">&nbsp;a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;v3(){&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">D::v3</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;vD(){&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">D::vD</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;&nbsp;}<BR>};</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>2)类图:</P>
<P><IMG 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022809481576.png"></P>
<P>&nbsp;</P>
<P>3)VS2008的编译选项查看布局:</P>
<P><IMG 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022809504724.png"></P>
<P>&nbsp;</P>
<P>4)可视化表示:</P>
<P><IMG 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022813172176.png"></P>
<P>&nbsp;</P>
<P>5)代码验证:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_100849 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_100849').style.display='none'; document.getElementById('Code_Open_Image_100849').style.display='inline'; document.getElementById('Code_Open_Text_100849').style.display='inline';" 
height=16 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif" 
width=11 align=top><IMG id=Code_Open_Image_100849 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_100849').style.display='none'; getElementById('Code_Closed_Image_100849').style.display='inline'; getElementById('Code_Closed_Text_100849').style.display='inline';" 
height=16 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif" 
width=11 align=top><SPAN class=cnblogs_code_Collapse 
id=Code_Closed_Text_100849>Code</SPAN><SPAN id=Code_Open_Text_100849><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN 
style="COLOR: #000000">typedef&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">Fun)();<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;PrintMember(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pI)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pI&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>}<BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;PrintVT(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVT)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVT&nbsp;</SPAN><SPAN 
style="COLOR: #000000">!=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)(pVT))();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pVT</SPAN><SPAN 
style="COLOR: #000000">++</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;PrintVTAndMember(B1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pD)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)pD;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVTB1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">0</SPAN><SPAN 
style="COLOR: #000000">);PrintVT(pVTB1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pMB1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;PrintMember(pMB1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVTB2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">2</SPAN><SPAN 
style="COLOR: #000000">);PrintVT(pVTB2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pMB2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;PrintMember(pMB2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVTB3&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">4</SPAN><SPAN 
style="COLOR: #000000">);PrintVT(pVTB3);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pMB3&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">5</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;PrintMember(pMB3);<BR>}<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;TestVT()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;B1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pB1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;D();<BR>&nbsp;&nbsp;&nbsp;&nbsp;D&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;dynamic_cast</SPAN><SPAN 
style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">D</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pD</SPAN><SPAN 
style="COLOR: #000000">-&gt;</SPAN><SPAN 
style="COLOR: #000000">x&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">10</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pD</SPAN><SPAN 
style="COLOR: #000000">-&gt;</SPAN><SPAN 
style="COLOR: #000000">y&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">20</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pD</SPAN><SPAN 
style="COLOR: #000000">-&gt;</SPAN><SPAN 
style="COLOR: #000000">z&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">30</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pD</SPAN><SPAN 
style="COLOR: #000000">-&gt;</SPAN><SPAN 
style="COLOR: #000000">a&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">40</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;PrintVTAndMember(pD);<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pD;<BR>}</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>6) 验证代码运行结果:</P>
<P><IMG 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022810105793.png"></P>
<P>&nbsp;</P>
<P>7)总结:</P>
<P><SPAN 
style="COLOR: #ff0000">与单继承相同的是所有的虚函数都包含在虚函数表中,所不同的多重继承有多个虚函数表,当子类对父类的虚函数有重写时,子类的函数覆盖父类的函数在对应的虚函数位置,当子类有新的虚函数时,这些虚函数被加在第一个虚函数表的后面。</SPAN></P>
<P>二 多重继承运行时类型转化</P>
<P>1)代码验证:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_101539 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_101539').style.display='none'; document.getElementById('Code_Open_Image_101539').style.display='inline'; document.getElementById('Code_Open_Text_101539').style.display='inline';" 
height=16 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif" 
width=11 align=top><IMG id=Code_Open_Image_101539 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_101539').style.display='none'; getElementById('Code_Closed_Image_101539').style.display='inline'; getElementById('Code_Closed_Text_101539').style.display='inline';" 
height=16 
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif" 
width=11 align=top><SPAN class=cnblogs_code_Collapse 
id=Code_Closed_Text_101539>Code</SPAN><SPAN id=Code_Open_Text_101539><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;TestDynamicCast()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;B1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pB1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;D();<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">B1:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pB1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;D&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;dynamic_cast</SPAN><SPAN 
style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">D</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">D:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;B2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pB2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;dynamic_cast</SPAN><SPAN 
style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">B2</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -