﻿<?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++博客-学海苦作舟，书山勤为径-随笔分类-C++</title><link>http://www.cppblog.com/windcsn/category/16488.html</link><description>留下点回忆行不行？</description><language>zh-cn</language><lastBuildDate>Thu, 14 Apr 2011 10:22:47 GMT</lastBuildDate><pubDate>Thu, 14 Apr 2011 10:22:47 GMT</pubDate><ttl>60</ttl><item><title>为什么C++的结构体或类没有成员是1个字节大小？</title><link>http://www.cppblog.com/windcsn/archive/2011/04/14/144174.html</link><dc:creator>笨笨</dc:creator><author>笨笨</author><pubDate>Thu, 14 Apr 2011 03:34:00 GMT</pubDate><guid>http://www.cppblog.com/windcsn/archive/2011/04/14/144174.html</guid><wfw:comment>http://www.cppblog.com/windcsn/comments/144174.html</wfw:comment><comments>http://www.cppblog.com/windcsn/archive/2011/04/14/144174.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/windcsn/comments/commentRss/144174.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/windcsn/services/trackbacks/144174.html</trackback:ping><description><![CDATA[本随笔紧接上个随笔：空结构体（类对于C++)的大小 <br><a href="http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html">http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html</a><br><br>这里试图讨论为什么C++的结构体或类即使没有成员也需要一个字节的大小？<br>首先需要一个字节的作用在上个随笔中已经说明，是为了在生成实例的时候确定区别；但实际上在C里面是不支持空成员的结构体，直接报编译错误。<br>因此这是C++特有的特性，可能有如下的原因：<br>1.C++里面必须支持没有成员的类，比如纯虚类；这些类只有方法，没有数据成员（虽然会有虚表）；但这些类不会被实例化。<br>2.为了达到统一，非纯虚类的C++结构体和类也会被C++支持，但他们需要实例化，所以需要提供一个字节来可以给他们分配内存。<br><br>而C里面是不支持没有成员的结构体（VC里面是编译不过的），其实完全没有必要支持这种无意义的定义，从而更简单。<br><br>欢迎讨论。 
<img src ="http://www.cppblog.com/windcsn/aggbug/144174.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/windcsn/" target="_blank">笨笨</a> 2011-04-14 11:34 <a href="http://www.cppblog.com/windcsn/archive/2011/04/14/144174.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>空结构体（类对于C++)的大小</title><link>http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html</link><dc:creator>笨笨</dc:creator><author>笨笨</author><pubDate>Wed, 13 Apr 2011 15:03:00 GMT</pubDate><guid>http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html</guid><wfw:comment>http://www.cppblog.com/windcsn/comments/144162.html</wfw:comment><comments>http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/windcsn/comments/commentRss/144162.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/windcsn/services/trackbacks/144162.html</trackback:ping><description><![CDATA[<p>类似代码：<br>typedef struct sD<br>{</p>
<p>}S_D;<br><br>int size_d&nbsp; = sizeof(S_D);<br><br>那么size_d=?<br><br>如果这是一段C++代码，那么这里结果是1；看上去编译器为这样没有成员的结构体隐形定义了一个一字节（char)的成员，目的是为了能用来标识类实例之间的不同。<br>这里可以这样理解，如果没有这一个字节，那么所有结构体的实例都是空，也就不占内存，那么他们的实例是没有区别的，因此C++的编译器不允许这么做，从而隐式增加了一个字节。<br><br>另外对于class也是同样的（因此C++中struct和class关键字基本没有区别）。<br><br>那么如果这个代码是C代码，最后的size_d是多少？<br></p>
<img src ="http://www.cppblog.com/windcsn/aggbug/144162.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/windcsn/" target="_blank">笨笨</a> 2011-04-13 23:03 <a href="http://www.cppblog.com/windcsn/archive/2011/04/13/144162.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于成员初始化列表的问题（C++）</title><link>http://www.cppblog.com/windcsn/archive/2011/04/10/143831.html</link><dc:creator>笨笨</dc:creator><author>笨笨</author><pubDate>Sat, 09 Apr 2011 16:47:00 GMT</pubDate><guid>http://www.cppblog.com/windcsn/archive/2011/04/10/143831.html</guid><wfw:comment>http://www.cppblog.com/windcsn/comments/143831.html</wfw:comment><comments>http://www.cppblog.com/windcsn/archive/2011/04/10/143831.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/windcsn/comments/commentRss/143831.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/windcsn/services/trackbacks/143831.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 关于成员初始化列表的问题（C++）；如果不注意可能导致程序问题。&nbsp;&nbsp;<a href='http://www.cppblog.com/windcsn/archive/2011/04/10/143831.html'>阅读全文</a><img src ="http://www.cppblog.com/windcsn/aggbug/143831.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/windcsn/" target="_blank">笨笨</a> 2011-04-10 00:47 <a href="http://www.cppblog.com/windcsn/archive/2011/04/10/143831.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>编译器上面时候为我们生成默认构造函数?(C++)</title><link>http://www.cppblog.com/windcsn/archive/2011/04/07/143566.html</link><dc:creator>笨笨</dc:creator><author>笨笨</author><pubDate>Thu, 07 Apr 2011 14:32:00 GMT</pubDate><guid>http://www.cppblog.com/windcsn/archive/2011/04/07/143566.html</guid><wfw:comment>http://www.cppblog.com/windcsn/comments/143566.html</wfw:comment><comments>http://www.cppblog.com/windcsn/archive/2011/04/07/143566.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/windcsn/comments/commentRss/143566.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/windcsn/services/trackbacks/143566.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 编译器上面时候为我们生成默认构造函数?(C++)&nbsp;&nbsp;<a href='http://www.cppblog.com/windcsn/archive/2011/04/07/143566.html'>阅读全文</a><img src ="http://www.cppblog.com/windcsn/aggbug/143566.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/windcsn/" target="_blank">笨笨</a> 2011-04-07 22:32 <a href="http://www.cppblog.com/windcsn/archive/2011/04/07/143566.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>