📄 [c++对象模型][9]虚继承与虚函数表 - itech's blog - 博客园.htm
字号:
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> vD2(){ cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D2::vD2</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">void</SPAN><SPAN
style="COLOR: #000000"> fD2(){ cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D2::fD2</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
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"> GD : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> D1, </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> D2<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"> vB(){ cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">GD::vB</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">void</SPAN><SPAN
style="COLOR: #000000"> vD1(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">GD::vD1</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"> vGD(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">GD::vGD</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">void</SPAN><SPAN
style="COLOR: #000000"> fGD(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">GD::fGD</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++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022810504770.png"></P>
<P> </P>
<P>3)VS2008的编译选项查看布局:</P>
<P><IMG
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022811200787.png"></P>
<P> </P>
<P>4)可视化表示:</P>
<P><IMG
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022812572527.png"></P>
<P> </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 </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 </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"> PrintMemberAndVT(GD </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pGD)<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">)pGD;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pD1VT </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">); <BR> (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pD1VT))(); (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pD1VT </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN
style="COLOR: #000000">))();<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVB </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: #800080">1</SPAN><SPAN
style="COLOR: #000000">); cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">vbtable's adress:</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVB </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pX </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">); PrintMember(pX);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pD2VT </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">3</SPAN><SPAN
style="COLOR: #000000">); <BR> (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pD2VT))();<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVB2 </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: #800080">4</SPAN><SPAN
style="COLOR: #000000">); cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">vbtable's adress:</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pVB2 </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pY </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">5</SPAN><SPAN
style="COLOR: #000000">); PrintMember(pY);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pA </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">6</SPAN><SPAN
style="COLOR: #000000">); PrintMember(pA);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pBVT </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">7</SPAN><SPAN
style="COLOR: #000000">); <BR> (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pBVT))();<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</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"> (pRoot </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">8</SPAN><SPAN
style="COLOR: #000000">); PrintMember(pI);<BR>}<BR><BR></SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -