📄 [c++对象模型][7]单继承与虚函数表 - itech's blog - 博客园.htm
字号:
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> f3(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">B::f3</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"> v3(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">B::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">int</SPAN><SPAN
style="COLOR: #000000"> y;<BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> C : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> B<BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> f3(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">C::f3</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;} </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 覆盖</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> v1(){cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">C::v1</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;} </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 重写</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> </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">C::v3</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;} </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 重写</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> z;<BR>};</SPAN><SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff"><BR></SPAN><SPAN
style="COLOR: #000000"></SPAN></SPAN></DIV>
<P> </P>
<P>2)类图:</P>
<P><IMG
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/2009022722083661.png"></P>
<P> </P>
<P>3)VS2008的编译选项查看布局:</P>
<P><IMG
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/2009022722105825.png"></P>
<P> </P>
<P>4)可视化表示:</P>
<P><IMG height=287
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/2009022722240651.png"
width=560></P>
<P> </P>
<P>5)代码验证:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_221141 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_221141').style.display='none'; document.getElementById('Code_Open_Image_221141').style.display='inline'; document.getElementById('Code_Open_Text_221141').style.display='inline';"
height=16
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_221141
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_221141').style.display='none'; getElementById('Code_Closed_Image_221141').style.display='inline'; getElementById('Code_Closed_Text_221141').style.display='inline';"
height=16
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_221141>Code</SPAN><SPAN id=Code_Open_Text_221141><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN
style="COLOR: #0000ff"></SPAN></SPAN><SPAN id=Code_Open_Text_220734><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"> PrintVTable(A </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pA)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</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"> (</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">(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pA);<BR> Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> pF </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> (Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pVT </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: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> iLength </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: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pF </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> NULL)<BR> {<BR> (</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pF)();<BR> </SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">iLength;<BR> pF </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> (Fun</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)(pVT </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN
style="COLOR: #000000"> iLength);<BR> }<BR>}<BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintMembers(A </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pA)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p </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">)(pA);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">1</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">while</SPAN><SPAN
style="COLOR: #000000">(i </SPAN><SPAN
style="COLOR: #000000"><=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">)<BR> {<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">(p</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN
style="COLOR: #000000">i) </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> i</SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">;<BR> }<BR>}<BR></SPAN></SPAN><SPAN
id=Code_Open_Text_221141><SPAN style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> TestVT()<BR>{<BR> A </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pA </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> C();<BR> C </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pC </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pA);<BR> pC</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> pC</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> pC</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> PrintVTable(pA);<BR> PrintMembers(pA);<BR> delete pA;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>6)验证代码运行结果:</P>
<P><IMG
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/2009022722265758.png"></P>
<P> </P>
<P>7)总结:</P>
<P><SPAN style="COLOR: #ff0000">单继承的对象的布局,第一个为虚函数表指针vtbl,</SPAN><SPAN
style="COLOR: #ff0000">其后为成员且先基类后子类,</SPAN><SPAN
style="COLOR: #ff0000">虚函数表里包含了所有的虚函数的地址,</SPAN><SPAN
style="COLOR: #ff0000">以NULL结束。</SPAN><SPAN
style="COLOR: #ff0000">虚函数如果子类有重写,就由子类的重新的代替。</SPAN></P>
<P>二 单继承运行时类型转化</P>
<P>1)代码验证:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_093106 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_093106').style.display='none'; document.getElementById('Code_Open_Image_093106').style.display='inline'; document.getElementById('Code_Open_Text_093106').style.display='inline';"
height=16
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_093106
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_093106').style.display='none'; getElementById('Code_Closed_Image_093106').style.display='inline'; getElementById('Code_Closed_Text_093106').style.display='inline';"
height=16
src="[C++对象模型][7]单继承与虚函数表 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_093106>Code</SPAN><SPAN id=Code_Open_Text_093106><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> A </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pA </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> C();<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">A:</SPAN><SPAN style="COLOR: #800000">"</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"> endl;<BR> B </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">B</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pA);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -