﻿<?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++博客-时光隧道-文章分类-object model</title><link>http://www.cppblog.com/thinke365/category/11669.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 07 Sep 2009 19:01:59 GMT</lastBuildDate><pubDate>Mon, 07 Sep 2009 19:01:59 GMT</pubDate><ttl>60</ttl><item><title>关键字说带来的差异</title><link>http://www.cppblog.com/thinke365/articles/95276.html</link><dc:creator>thinke365</dc:creator><author>thinke365</author><pubDate>Thu, 03 Sep 2009 17:49:00 GMT</pubDate><guid>http://www.cppblog.com/thinke365/articles/95276.html</guid><wfw:comment>http://www.cppblog.com/thinke365/comments/95276.html</wfw:comment><comments>http://www.cppblog.com/thinke365/articles/95276.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/thinke365/comments/commentRss/95276.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/thinke365/services/trackbacks/95276.html</trackback:ping><description><![CDATA[<p>如果不是为了维护和C之间的兼容性，C++ 远可以比现在更简单些。<br><br>strut关键字，其实没有什么用&#8230;&#8230;<br>那通常什么时候，一个人应该使用struct取代class。<br>答案是：当它使一个人感觉比较好的时候。</p>
<p><span style="COLOR: #0000ff">class A { public: .... } 和struct A {} 是对等的</span>。 <br><br>struct or class {<br>public:<br>&nbsp;&nbsp; ...<br>protected:<br>&nbsp; ...<br>}&nbsp; // <span style="COLOR: #0000ff">事实上可以说上面的东西是struct，也可以说是class</span>。这两种声明的观念上的意义取决于对"声明"本身的检验。 <span style="FONT-SIZE: 8pt; COLOR: #ff0000">struct里面也可以放private?</span> 这个倒是没有试过<br><br>cfront是C++的第一个实现，由Lippman完成。 struct和class关键字，在语义分析器(parser)中是以共享的"AGGR"替换的。<br><br>如果语言只支持一个关键字，就可以省去很多的混淆和迷惑。<br>但<span style="FONT-SIZE: 10pt; COLOR: #0000ff">C++要支持现存的C程序代码，就不能不支持struct</span><br><span style="FONT-SIZE: 10pt; COLOR: #ff0000">那么一定要引入新关键字class吗</span>? 不，但是引入它的确非常令人满意。<br>因为引入的不只是关键字，<span style="FONT-SIZE: 10pt; COLOR: #ff0000">还有它所支持的封装和继承的哲学</span>。<br><span style="COLOR: #ff0000">可以说，struct关键字的使用伴随着一个public接口的声明</span>。<strong>甚至可以说，struct的用途只是为了方便C程序员迁移至C++部落</strong>。<br><br></p>
<img src ="http://www.cppblog.com/thinke365/aggbug/95276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/thinke365/" target="_blank">thinke365</a> 2009-09-04 01:49 <a href="http://www.cppblog.com/thinke365/articles/95276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多种C++对象模型</title><link>http://www.cppblog.com/thinke365/articles/95258.html</link><dc:creator>thinke365</dc:creator><author>thinke365</author><pubDate>Thu, 03 Sep 2009 13:30:00 GMT</pubDate><guid>http://www.cppblog.com/thinke365/articles/95258.html</guid><wfw:comment>http://www.cppblog.com/thinke365/comments/95258.html</wfw:comment><comments>http://www.cppblog.com/thinke365/articles/95258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/thinke365/comments/commentRss/95258.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/thinke365/services/trackbacks/95258.html</trackback:ping><description><![CDATA[<p>再复杂的系统，在设计之处，总是很简单的。C++编译器的实现也是如此。<br><br>Lippman的第一个模型十分简单。 是为了<span style="FONT-SIZE: 10pt; COLOR: #0000ff">尽可能降低C++编译器的设计复杂度</span>而开发出来的，<span style="COLOR: #0000ff">赔上的则是空间和执行期效率</span>。<br>简单模型中， members本身并不放在object之中，只有指向member的指针才放在object内部。这么做可以避免对象有不同类型，而需要不同存储空间的问题。 <span style="COLOR: #0000ff">Object中的members是以slot索引值来寻址的</span>。 在这种对象模型中，class object的大小很容易计算出来。指针大小乘以members数目就可以了。 <span style="FONT-SIZE: 10pt; COLOR: red">虽然这个模型没有被用到实际产品中去，但是关于索引或slot数目的观念，倒是被应用到C++"<span style="BACKGROUND-COLOR: yellow">指向成员的指针</span>"观念之中</span>。<br><br>表格驱动的对象模型。<br>每个类对应于两张表 data member table和<span style="COLOR: red">member function table</span>。<br>data member table 中直接放数据，<span style="COLOR: #0000ff">member function table则存放slot，这些slot在指向funtion</span>。<br>这个模型也没有实际应用到真正的C++编译器上，而<span style="FONT-SIZE: 10pt; COLOR: #008000; BACKGROUND-COLOR: #ffffff">member function table这个观念则成为 virtual function的一个有效方案</span>。 virtual function还有其他什么实现方案吗? 各个编译器会采用不同的实现方案吗?&nbsp; data member最后的实现，不是用 表进行组织的吗?<br><br>C++对象模型。<br>Stroustrup当初设计C++对象模型是从简单对象模型派生而来的，并对内存空间和存取时间做了优化。<br>Nonstatic data members被配置在每一个 class object之内，static data members 则被存放在所有的class object 之外。<br><br><span style="FONT-SIZE: 10pt; COLOR: #0000ff">new出来的对象放在堆中，而普通的变量这是放在栈中(函数调用时候的栈)。 对此的理解好像有更深入了一些</span>。<br><br>每个class有一个 virtual table <span style="COLOR: #ff0000"><strong>vtbl</strong></span>,里面存放着一堆指向 member function的指针。&nbsp;&nbsp; <span style="FONT-SIZE: 10pt; COLOR: #0000ff">每个class object被添加了一个指针，指向相关的 virtual table</span>，通常这个指针被称为 <span style="COLOR: #0000ff"><strong>vptr</strong></span>。 vptr的设置和重置，由类的 constructor, distructor和copy assignment运算符自动完成。 (具体在第五章讨论?)<br><br><span style="COLOR: #0000ff">每个class关联的type_info_object 也放在 virtual table中</span>， 用来支持运行时类型识别 runtime type identification, RTTI， 通常放在virtual table的第一个slot处<br>这个模型和表格驱动模型稍微有点区别， 表格驱动模型， 数据成员也是通过一张表进行查找的，而这里的C++对象模型，则直接把数据放到了对象之中。<br><br>这种设计的缺点是，数据成员修改(添加、删除或修改数据成员)，程序代码也需要重新编译。而通过两张表的话，程序代码是不用重新编译的。<br>(还要和之前的代码进行比对，看是否修改了函数?呵呵，一修改就重新编译，显得更简单吧。) 还有<span style="FONT-SIZE: 10pt; COLOR: red">用两张表，多了一层间接性，有更大弹性，但是需要付出空间和执行效率两方面的代价</span>。<br><br>加入继承后的考虑：<br>C++对象模型的继承机制，原来不运用任何间接性。 这样可以对base class member进行最紧凑而且有效的存取。 缺点是， base class的任何改变，都会导致 base class和derived class的重新编译。&nbsp;&nbsp;&nbsp; ---&gt;&nbsp; 使用间接机制呢? 有时应该也需要重新编译子类的吧，如子类用到一个父类的方法，而父类中这个方法已经被删除。。。&nbsp; 如果仅仅是修改内部，可能子类不需要重新编译，只需要重新连接就可以了。<br><br><br></p>
<img src ="http://www.cppblog.com/thinke365/aggbug/95258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/thinke365/" target="_blank">thinke365</a> 2009-09-03 21:30 <a href="http://www.cppblog.com/thinke365/articles/95258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对象</title><link>http://www.cppblog.com/thinke365/articles/95150.html</link><dc:creator>thinke365</dc:creator><author>thinke365</author><pubDate>Wed, 02 Sep 2009 15:54:00 GMT</pubDate><guid>http://www.cppblog.com/thinke365/articles/95150.html</guid><wfw:comment>http://www.cppblog.com/thinke365/comments/95150.html</wfw:comment><comments>http://www.cppblog.com/thinke365/articles/95150.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/thinke365/comments/commentRss/95150.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/thinke365/services/trackbacks/95150.html</trackback:ping><description><![CDATA[使用宏比使用函数，可以获得更高的效率。 哪些地方会使用这种技巧? 调用次数很多的地方，通过减少函数调用，来提升性能?<br><br>使用inline，经过编译之后也没有函数调用? 这个和宏有什么区别? 宏是编译之前的预处理，那么inline是什么处理呢?<br><br>加上封装后的布局(Layout Costs for Adding Encapsulation)<br>一般情况下不会产生负担。<br>data member直接含在class object之中，就像C struct的情况一样。<br>而member function虽然含在class声明之内，却不出现在object之中，<span style="FONT-SIZE: 10pt; COLOR: red">每一个non-inline member function只会诞生一个函数实体</span>。<br>而inline function则会在其每一个使用者(模块)身上产生一个函数实体。<br><br>virtual function 机制：<span style="FONT-SIZE: 10pt; COLOR: red">用以支持一个有效率的"执行期绑定"</span>。 (runtime binding)<br>virtual base class 机制: 实现多次出现在继承体系中的base class，<span style="FONT-SIZE: 10pt; COLOR: red">有一个单一而被共享的实体。<br><br><span style="COLOR: #ff0000">还有一些多重继承下的额外负担<br>一般而言，并没有天生的理由说C++程序一定比C兄弟庞大或迟缓<br><br></span></span>
<img src ="http://www.cppblog.com/thinke365/aggbug/95150.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/thinke365/" target="_blank">thinke365</a> 2009-09-02 23:54 <a href="http://www.cppblog.com/thinke365/articles/95150.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>前言</title><link>http://www.cppblog.com/thinke365/articles/95134.html</link><dc:creator>thinke365</dc:creator><author>thinke365</author><pubDate>Wed, 02 Sep 2009 13:21:00 GMT</pubDate><guid>http://www.cppblog.com/thinke365/articles/95134.html</guid><wfw:comment>http://www.cppblog.com/thinke365/comments/95134.html</wfw:comment><comments>http://www.cppblog.com/thinke365/articles/95134.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/thinke365/comments/commentRss/95134.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/thinke365/services/trackbacks/95134.html</trackback:ping><description><![CDATA[如果程序员了解底层实现，他就能写出效率较高的代码。<br><br>写C++对象模型这本书的一个目的是为了消除人们对C++语言的各种错误认识。<br><span style="FONT-SIZE: 10pt; COLOR: #3366ff">之前有人认为，如果要写一个快速而有效率的数据库引擎，应该使用C而非C++，他认为C++庞大又迟缓</span>。<br><br>这本书将尽可能多地<span style="BACKGROUND-COLOR: yellow">讲解 各种 Object facility 所带来的额外负荷</span>。<br><br>
<img src ="http://www.cppblog.com/thinke365/aggbug/95134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/thinke365/" target="_blank">thinke365</a> 2009-09-02 21:21 <a href="http://www.cppblog.com/thinke365/articles/95134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>