📄 [c++对象模型][8]多重继承与虚函数表 - itech's blog - 博客园.htm
字号:
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl; }<BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> D : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> B1, </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> B2, </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> B3<BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> a;<BR> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> v3(){ cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D::v3</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl; }<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> vD(){ cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D::vD</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl; }<BR>};</SPAN></SPAN></DIV>
<P> </P>
<P> </P>
<P>2)类图:</P>
<P><IMG
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022809481576.png"></P>
<P> </P>
<P>3)VS2008的编译选项查看布局:</P>
<P><IMG
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022809504724.png"></P>
<P> </P>
<P>4)可视化表示:</P>
<P><IMG
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022813172176.png"></P>
<P> </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 </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">Fun)();<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintMember(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pI)<BR>{<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pI </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}<BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintVT(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVT)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVT </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> NULL)<BR> {<BR> (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pVT))();<BR> pVT</SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">;<BR> }<BR>}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintVTAndMember(B1 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pD)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pRoot </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)pD;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVTB1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</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 </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">);PrintVT(pVTB1);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pMB1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> pRoot </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN
style="COLOR: #000000">; PrintMember(pMB1);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVTB2 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</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 </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">2</SPAN><SPAN
style="COLOR: #000000">);PrintVT(pVTB2);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pMB2 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> pRoot </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">; PrintMember(pMB2);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVTB3 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</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 </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">4</SPAN><SPAN
style="COLOR: #000000">);PrintVT(pVTB3);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pMB3 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> pRoot </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">5</SPAN><SPAN
style="COLOR: #000000">; PrintMember(pMB3);<BR>}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> TestVT()<BR>{<BR> B1 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pB1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> D();<BR> D </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pD </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">D</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB1);<BR> pD</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">x </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">10</SPAN><SPAN
style="COLOR: #000000">;<BR> pD</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">y </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">20</SPAN><SPAN
style="COLOR: #000000">;<BR> pD</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">z </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">30</SPAN><SPAN
style="COLOR: #000000">;<BR> pD</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">a </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">40</SPAN><SPAN
style="COLOR: #000000">;<BR> PrintVTAndMember(pD);<BR> delete pD;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P> </P>
<P>6) 验证代码运行结果:</P>
<P><IMG
src="[C++对象模型][8]多重继承与虚函数表 - iTech's Blog - 博客园.files/2009022810105793.png"></P>
<P> </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"> TestDynamicCast()<BR>{<BR> B1 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pB1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> D();<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">B1:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pB1 </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> D </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pD </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">D</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB1);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pD </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> B2 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pB2 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">B2</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB1);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -