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

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

📁 最为全面的c++内存模型探秘。不管是初学者还是老手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;TestVT()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;B&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;GD();<BR>&nbsp;&nbsp;&nbsp;&nbsp;GD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pGD&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">GD</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pGD</SPAN><SPAN 
style="COLOR: #000000">-&gt;</SPAN><SPAN 
style="COLOR: #000000">i&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;pGD</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">20</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pGD</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">30</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pGD</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;PrintMemberAndVT(pGD);<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pGD;<BR>}</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>6)验证代码结果:</P>
<P><IMG 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022812593163.png"></P>
<P>&nbsp;</P>
<P>7)总结:</P>
<P><SPAN 
style="COLOR: #ff0000">虚继承,使公共的基类在子类中只有一份,我们看到虚继承在多重继承的基础上多了vbtable来存储到公共基类的偏移。</SPAN></P>
<P>&nbsp;</P>
<P>二 虚继承运行时类型转化 </P>
<P>&nbsp;</P>
<P>1)代码验证:<IMG id=Code_Closed_Image_130505 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_130505').style.display='none'; document.getElementById('Code_Open_Image_130505').style.display='inline'; document.getElementById('Code_Open_Text_130505').style.display='inline';" 
height=16 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif" 
width=11 align=top><SPAN id=Code_Open_Text_130505><SPAN 
style="COLOR: #000000"></SPAN></SPAN></P>
<P>&nbsp;</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_130736 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_130736').style.display='none'; document.getElementById('Code_Open_Image_130736').style.display='inline'; document.getElementById('Code_Open_Text_130736').style.display='inline';" 
height=16 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/ContractedBlock.gif" 
width=11 align=top><IMG id=Code_Open_Image_130736 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_130736').style.display='none'; getElementById('Code_Closed_Image_130736').style.display='inline'; getElementById('Code_Closed_Text_130736').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_130736>Code</SPAN><SPAN id=Code_Open_Text_130736><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;B&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;GD();<BR>&nbsp;&nbsp;&nbsp;&nbsp;GD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pGD&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">GD</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB);<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">GD:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pGD&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;D1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pD1&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">D1</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB);<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">D1:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pD1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;D2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pD2&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">D2</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pB);<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">D2:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pD2&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<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">B:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pB&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/2009022813070883.png"></P>
<P>&nbsp;</P>
<P>3)总结:</P>
<P><SPAN 
style="COLOR: #ff0000">还是从内存布局来看dynamic_cast时地址的变化,第一个基类的地址与子类相同,其他的基类和虚基类需要做偏移。</SPAN></P>
<P>三 完!</P>
<DIV id=MySignature>
<P>感谢,Thanks!<BR><BR>作者:<A href="http://itech.cnblogs.com/">iTech</A><BR>出处:<A 
href="http://itech.cnblogs.com/">http://itech.cnblogs.com/</A> 
<BR>转载:本文版权归作者iTech所有,转载请注明出处,不得用于商业用途!</P></DIV>
<DIV id=EntryTag>Tag标签: <A 
href="http://www.cnblogs.com/itech/tag/[Cpp对象模型]/">[Cpp对象模型]</A></DIV></DIV>
<DIV class=postDesc>posted @ 2009-03-01 19:35 <A 
href="http://www.cnblogs.com/itech/">iTech</A> 阅读(49) <A 
href="http://www.cnblogs.com/itech/archive/2009/02/27/1399996.html#Post">评论(0)</A> 
&nbsp;<A 
href="http://www.cnblogs.com/itech/admin/EditPosts.aspx?postid=1399996">编辑</A> 
<A href="http://www.cnblogs.com/itech/AddToFavorite.aspx?id=1399996">收藏</A> <A 
onclick="PutInWz();return false;" 
href="http://www.cnblogs.com/itech/archive/2009/02/27/1399996.html#">网摘</A> 
所属分类: <A href="http://www.cnblogs.com/itech/category/170009.html">C++</A> 
</DIV></DIV><IMG height=1 
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/1399996.jpg" width=1> <!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.cnblogs.com/itech/archive/2009/03/01/1399996.html"
dc:identifier="http://www.cnblogs.com/itech/archive/2009/03/01/1399996.html"
dc:title=""
trackback:ping="http://www.cnblogs.com/itech/services/trackbacks/1399996.aspx" />
</rdf:RDF>
--></DIV><!--end: topics 文章、评论容器-->
<DIV id=AjaxHolder_UpdatePanel1>
<STYLE>TD {
	FONT-SIZE: 12px
}
.commentTextBox {
	FONT-SIZE: 13px; FONT-FAMILY: Verdana
}
A.blue:visited {
	COLOR: blue
}
A.blue:active {
	COLOR: blue
}
A.blue:link {
	COLOR: blue
}
A.blue:hover {
	COLOR: blue
}
</STYLE>
<!--Beging Temp Save-->
<STYLE>.userData {
	BEHAVIOR: url(#default#userdata)
}
</STYLE>

<DIV class=userData id=CommentsPersistDiv></DIV>
<SCRIPT type=text/javascript>
    function pageLoad() {
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
        //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
    }

    function handleInitializeRequest(sender, args) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        var eid = args.get_postBackElement().id;
        /*if (eid.indexOf("DeleteLink")>0) 
        {
        args.get_postBackElement().innerHTML = "<font color='red'>正在删除...</font>";         
        }    
        else */
        if (eid.indexOf("btnSubmit") > 0) {
            document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML = "正在提交...";
            document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
        }
        else if (eid.indexOf("refreshList") > 0) {
            document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML = "<font color='red'>正在刷新...</font>";
        }

    }

    function TempSave(ElementID) {
        try {
            CommentsPersistDiv.setAttribute("CommentContent", document.getElementById(ElementID).value);
            CommentsPersistDiv.save("CommentXMLStore");
        }
        catch (ex) {
        }

    }
    function Restore(ElementID) {
        CommentsPersistDiv.load("CommentXMLStore");
        document.getElementById(ElementID).value = CommentsPersistDiv.getAttribute("CommentContent");
    }	
			
			
</SCRIPT>
<!--Ene TempSave-->
<DIV id=divRefreshComments 
style="FONT-SIZE: 12px; MARGIN-BOTTOM: 5px; MARGIN-RIGHT: 10px; TEXT-ALIGN: right"><A 
id=AjaxHolder_PostComment_refreshList 
href="javascript:__doPostBack('AjaxHolder$PostComment$refreshList','')">刷新评论列表</A>&nbsp;&nbsp;</DIV>
<DIV class=commentform><SPAN id=AjaxHolder_PostComment_ltSubmitMsg 
style="COLOR: red"></SPAN><BR><A name=Feedback></A>
<TABLE style="WIDTH: 600px" cellSpacing=1 cellPadding=1 border=0>
  <TBODY>
  <TR>
    <TD colSpan=3>
      <DIV id=AjaxHolder_PostComment_panelADUper></DIV></TD></TR>
  <TR>
    <TD></TD>
    <TD></TD>

⌨️ 快捷键说明

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