﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-云淡风轻</title><link>http://www.cppblog.com/cloudsky12/</link><description>开发的乐趣</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 21:15:15 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 21:15:15 GMT</pubDate><ttl>60</ttl><item><title>C++的虚函数表问题</title><link>http://www.cppblog.com/cloudsky12/archive/2009/08/11/92972.html</link><dc:creator>阳荣辉</dc:creator><author>阳荣辉</author><pubDate>Tue, 11 Aug 2009 15:10:00 GMT</pubDate><guid>http://www.cppblog.com/cloudsky12/archive/2009/08/11/92972.html</guid><wfw:comment>http://www.cppblog.com/cloudsky12/comments/92972.html</wfw:comment><comments>http://www.cppblog.com/cloudsky12/archive/2009/08/11/92972.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cloudsky12/comments/commentRss/92972.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cloudsky12/services/trackbacks/92972.html</trackback:ping><description><![CDATA[<div>请看下面的代码。<span style="COLOR: #000000">
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;A<br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;A(){a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;}<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">A()&nbsp;{};<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a;<br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;B&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;A<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;B(){b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;}<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">virtual</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">B(){}<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&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;Func(){}<br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b;<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;B</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;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;B;<br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;A</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pA&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pB;<br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b=%p&nbsp;a=%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;pB,&nbsp;pA);<br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #000000"></span></div>
</span>输出是：<span style="COLOR: red">b=00372D98 a=00372D9C</span> <br>为什么两个指针的赋值都会不一样？<br>让我们看看这两块内存吧。<br><br><img src="http://m358.mail.qq.com/cgi-bin/read_pic?sid=XHYFGdhe*clIAgNJ&amp;pid=ZC0000J7dGE8p9TQqISFlKCdWtgg69" ow="292"><br><img src="http://m358.mail.qq.com/cgi-bin/read_pic?sid=XHYFGdhe*clIAgNJ&amp;pid=ZC0000Z2LCtXogSRqqLwMZipe61A69" ow="391"><br>原来pB的内存前多了４字节的虚函数表。<br>因为Ａ没有虚函数。Ａ就没有虚函数表。<br>但是Ｂ是有虚函数表的，因此在Ｂ对象的内存中，会多一块内存来存放虚函数表。&nbsp;<br>如果通过delete pA来释放Ｂ的话就会造成内存泄漏。<br>因此在设计类的时候，除非确定不会被继承，否则<font color=#ff0000>释构函数一定要声明成虚函数</font>。</div>
<img src ="http://www.cppblog.com/cloudsky12/aggbug/92972.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cloudsky12/" target="_blank">阳荣辉</a> 2009-08-11 23:10 <a href="http://www.cppblog.com/cloudsky12/archive/2009/08/11/92972.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>