📄 [c++对象模型][9]虚继承与虚函数表 - itech's blog - 博客园.htm
字号:
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> TestVT()<BR>{<BR> B </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> GD();<BR> GD </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pGD </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">GD</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB);<BR> pGD</SPAN><SPAN
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">i </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">10</SPAN><SPAN
style="COLOR: #000000">;<BR> pGD</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">20</SPAN><SPAN
style="COLOR: #000000">;<BR> pGD</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">30</SPAN><SPAN
style="COLOR: #000000">;<BR> pGD</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> PrintMemberAndVT(pGD);<BR> delete pGD;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>6)验证代码结果:</P>
<P><IMG
src="[C++对象模型][9]虚继承与虚函数表 - iTech's Blog - 博客园.files/2009022812593163.png"></P>
<P> </P>
<P>7)总结:</P>
<P><SPAN
style="COLOR: #ff0000">虚继承,使公共的基类在子类中只有一份,我们看到虚继承在多重继承的基础上多了vbtable来存储到公共基类的偏移。</SPAN></P>
<P> </P>
<P>二 虚继承运行时类型转化 </P>
<P> </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> </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"> TestDynamicCast()<BR>{<BR> B </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> GD();<BR> GD </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pGD </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">GD</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">GD:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pGD </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> D1 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pD1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">D1</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D1:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pD1 </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> D2 </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pD2 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">D2</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pB);<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">D2:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pD2 </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">B:</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> pB </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/2009022813070883.png"></P>
<P> </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>
<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> </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 + -