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

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

📁 最为全面的c++内存模型探秘。不管是初学者还是老手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;vD2(){&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">D2::vD2</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;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;fD2(){&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">D2::fD2</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;}<BR>};<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;GD&nbsp;:&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;D1,&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;D2<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;vB(){&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">GD::vB</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;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;vD1(){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">GD::vD1</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;}<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;vGD(){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">GD::vGD</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;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;fGD(){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">GD::fGD</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;}<BR>};</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>2)类图:</P>
<P><IMG 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022810504770.png"></P>
<P>&nbsp;</P>
<P>3)VS2008的编译选项查看布局:</P>
<P><IMG 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022811200787.png"></P>
<P>&nbsp;</P>
<P>4)可视化表示:</P>
<P><IMG 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022812572527.png"></P>
<P>&nbsp;</P>
<P>5)代码验证:(<SPAN style="COLOR: #ff0000">此时的虚函数表不是以NULL结尾,为什么?</SPAN>)</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_125828 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_125828').style.display='none'; document.getElementById('Code_Open_Image_125828').style.display='inline'; document.getElementById('Code_Open_Text_125828').style.display='inline';" 
height=16 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif" 
width=11 align=top><IMG id=Code_Open_Image_125828 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_125828').style.display='none'; getElementById('Code_Closed_Image_125828').style.display='inline'; getElementById('Code_Closed_Text_125828').style.display='inline';" 
height=16 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif" 
width=11 align=top><SPAN class=cnblogs_code_Collapse 
id=Code_Closed_Text_125828>Code</SPAN><SPAN id=Code_Open_Text_125828><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&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;PrintMemberAndVT(GD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pGD)<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">)pGD;<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">pD1VT&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">);&nbsp;<BR>&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">)(pD1VT))();&nbsp;(</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)(pD1VT&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">))();<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">pVB&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: #800080">1</SPAN><SPAN 
style="COLOR: #000000">);&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">vbtable's&nbsp;adress:</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<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">pX&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">2</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;PrintMember(pX);<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">pD2VT&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">3</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;<BR>&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">)(pD2VT))();<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">pVB2&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: #800080">4</SPAN><SPAN 
style="COLOR: #000000">);&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">vbtable's&nbsp;adress:</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pVB2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<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">pY&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">5</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;PrintMember(pY);<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">pA&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">6</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;PrintMember(pA);<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">pBVT&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">7</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;<BR>&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">)(pBVT))();<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">pI&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;(pRoot&nbsp;</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">8</SPAN><SPAN 
style="COLOR: #000000">);&nbsp;PrintMember(pI);<BR>}<BR><BR></SPAN><SPAN 

⌨️ 快捷键说明

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