词条 | Effective C++:改善程序与设计的55个具体做法:第3版 |
释义 | 图书信息传世经典书丛 Effective C++:改善程序与设计的55个具体做法:第3版 (美)梅耶(Meyers,S.)著 云风评注 ISBN 978-7-121-13376-3 2011年6月出版 定 价:65.00元 16开 340页 内 容 简 介“C++程序员可以分成两类,读过Effective C++的和没读过的。”世界顶级C++大师Scott Meyers这部成名之作,与这句话一道在全球无数读者间广为传颂。几乎所有C++书籍推荐名单上﹐本书都会位列三甲。作者高超的技术把握力、独特的视角﹑诙谐轻松的写作风格﹑独具匠心的内容组织﹐都受到极大的推崇和仿效。 对于国外技术图书,选择翻译版还是影印版,常让人陷入两难。本评注版力邀国内资深专家执笔,在英文原著基础上增加中文点评与注释,旨在融合二者之长,既保留经典的原创文字与味道,又以先行者的学研心得与实践感悟,对读者阅读与学习加以点拨、指明捷径。 经过评注的版本,更值得反复阅读与体会。希望这本书能够帮助您跨越C++的重重险阻,领略高处才有的壮美风光,做一个成功而快乐的C++程序员。 评注者序2010年秋,电子社编辑侠少寄给我一本Effective C++(第三版)英文原版书,并托我为这本书写一些评注,希望做成评注版在国内出版。经慎重考虑后我受领了这一任务。 与Effective C++的渊源 回想起来,通过更深入地理解C++而获得一种喜悦感,已是十多年前的事情了。2000 年前后我刚刚从C语言迁移到C++来做实际的项目,同一时期国内涌现出一大批C++语言相关著作。我读了一本便一发不可收拾,几乎阅遍当时可以找到的相关书籍。从纯粹学习语言的角度来讲,Effective C++是相当重要的一本书,也是作为过来人的我最想推荐给大家的。阅读其时(当时是第二版),我已经用C++编写过一个开源的游戏引擎,有了数万行代码的经验,书中总结的条款读来心有戚戚焉。犹记得当年是在书店驻足读完大半本书,才想起来买下来带回去读。 时光堆积的反思 之后的几年里,我用C++编写了数十万行代码。写得越多,对之前的作品越不满意,代码风格也随之变化,而且慢慢产生怀疑,到底是否存在一种普遍合理、高效的C++使用方法——它可以让其他程序员,或是将来的自己审阅代码后,表示一致赞赏,而不需要用无休止的重构来满足自己的完美主义倾向。其实也就是本书作者Scott Meyers这些年来探讨的主题。C++伴随软件工业发展这些年,修修补补。由于工程需要,它必须保持向前兼容,并尽量满足每个时期的工程需求。这注定它不可能是一门完美的语言,也注定它是一门备受争议的语言。 尽管对C++的批评和质疑从未间断,但毋庸置疑,它绝不会销声匿迹。而且,C++到底是不是一门高效语言,并不是本书探讨的重点。本书的着眼点在于,如果你选择了C++,如何使它变得更高效,也即如何有效使用C++。其实,一开始我是有些好奇,大牛Scott Meyers这个主题写了十多年,就没有什么可抱怨的?当这次为点评而重新读到Item 25——由std::swap的扩展问题(本书第117页)引申到特例化std名字空间里的方法时,一 句“Alas, the form of the prohibition may dismay you”惹我会心一笑,从此释然。 再读经典的别样感触 再读这本书的新版,速度很慢,有时还会和第二版对照一下,体会作者思想的变迁。作为参考,还重温了《C++语言的设计和演化》的几个章节。毕竟文字最终要印成铅字,不由得慎重起来,似乎从来没有如此耐心地逐字读英文句子。读到细节处,发现作者把每个问题讲得都很透,表达流畅,前后反复呼应,即使无太多英文阅读经验的人,也可以轻松读懂,不愧是写了十年的精品。十分钦佩之余,却也为无从下笔而犯难,似乎能任意发挥之处,只剩对C++的争议。初稿发给出版社后,经编辑提醒又增加一些“帮助初学者解惑和提速”的内容,希望不致误导C++初学者放弃学习这门有趣的语言。 不安的评注者评注这个工作比翻译难做,尤其是评Effective C++这样的经典。写得太多有狗尾续貂之嫌,太少又愧对读者的期望。而且,无论怎样写,都会带有特定时期个人观点的局限性。要特别提醒的是,不要把某些评注看作是对C++的批评。作为用了多年C++并一度沉迷其中的程序员,那只是爱之深后的责之切。 本书除了少部分评注是针对个别代码段或关键词外,尚有不少篇幅为对原书篇章、段落主题的拓展思考,可抛开原文独立阅读。限于水平,有很多地方,想表达的东西没能讲透,是一种遗憾。相对于Scott Meyers积累了十数年的精华章句,我这个后学晚辈仓促成文忝列其间,实在诚惶诚恐。评注中错误之处,望方家或不吝赐教,或一笑了之。 祝各位读者拥有和我一样愉快的阅读体验。 云风 2011 年春 于 杭州 序 言1991年我写下Effective C++第一版。1997年撰写第二版时我更新了许多重要内容,但为了不让熟悉第一版的读者感到困惑,我竭尽所能保留原始结构:原先50个条款中的48个标题基本没变。如果把书籍视为一栋房屋,第二版只是更换地毯、灯饰,重新粉刷一遍而已。 到了第三版,修缮工作进一步深入壁骨墙筋(好几次我甚至希望能够翻新地基)。1991年起C++世界经历了巨大变革,而本书目标——在一本小而有趣的书中确认最重要的一些C++编程准则——却已不再能够由15年前建立的那些条款体现出来。“C++程序员拥有C背景”这句话在1991年是个合理假设,如今C++程序员却很可能来自Java或C#阵营。继承(inheritance)和面向对象编程(object-oriented programming)在1991年对大多数程序员都很新鲜,如今程序员已经建立良好概念,异常(exceptions)、模板(templates)和泛型编程(generic programming)才是需要更多引导的领域。1991年没人听过所谓设计模式(design patterns),如今少了它很难讨论软件系统。1991年C++正式标准才刚要上路,如今C++标准规范已经8岁,新版规范蓄势待发†。 为了对付这些改变,我把所有条款抹得一干二净,然后问自己“2005年什么是对C++ 程序员最重要的忠告?”答案便是第三版中的这些条款。本书有两个新章,一个是资源管理(resource management),一个是模板编程(programming with templates)。实际上template这东西遍布全书,因为它们几乎影响了C++的每个角落。本书新素材还包括在exceptions概念下编程、套用设计模式,以及运用新的TR1程序库设施(TR1于条款54描述)。本书也告诉大家在单线程系统(single-threaded systems)中运行良好但可能不适用于多线程系统(multithreaded systems)的某些技术和做法。本书半数以上内容是新的。在此同时第二版大部分基础信息仍然很重要,所以我找出一个保留它们的办法:你可以在附录B找到第二、第三两版的条款对应表。 我努力让本书达到我所能够达到的最佳状态,但这并不表示它已臻完美。如果你认为某些条款不适合作为一般性忠告,或你有更好的办法完成本书所谈的某件工作,或书中某些技术讨论不够清楚不够完全,甚或有所误导,请告诉我。如果你找出任何错误——技术上的、文法上的、排版印刷上的,不论哪一种——也请告诉我。我很乐意将第一位提出问题并吸引我注意的朋友加入下次印刷的致谢名单中。 即使本书条款个数扩充为55,这一整组编程准则还谈不上完备。然而毕竟整理出优良准则——几乎任何时间适用于任何应用程序的准则——比想象中困难得多。如果你有其他编程准则的想法或建议,我将乐以与闻。 我手上维护本书第一印以来的变化清单,其中包括错误修订、进一步说明和技术更新。这份清单放在网址为"Effective C++ Errata"网页上。如果你希望在这份清单更新时获得通知,请加入我的邮件列表。这份列表用来发布消息给可能对我的专业工作感兴趣的人士。 Scott Douglas Meyers Stafford.Oregon 2005年4月 侯捷 译 目 录Introduction(新增批注共2条) 1 Chapter 1: Accustoming Yourself to C++ (新增批注共12条) 11 Item 1: View C++ as a federation of languages. 11 Item 2: Prefer consts, enums, and inlines to #defines. 14 Item 3: Use const whenever possible. 19 Item 4: Make sure that objects are initialized before they’re used. 28 Chapter 2: Constructors, Destructors, and Assignment Operators(新增批注共9条) 37 Item 5: Know what functions C++ silently writes and calls. 37 Item 6: Explicitly disallow the use of compiler-generated functions you do not want. 41 Item 7: Declare destructors virtual in polymorphic base classes. 43 Item 8: Prevent exceptions from leaving destructors. 49 Item 9: Never call virtual functions during construction or destruction. 53 Item 10: Have assignment operators return a reference to *this. 57 Item 11: Handle assignment to self in operator=. 58 Item 12: Copy all parts of an object. 62 Chapter 3: Resource Management(新增批注共7条) 66 Item 13: Use objects to manage resources. 66 Item 14: Think carefully about copying behavior in resource-managing classes. 71 Item 15: Provide access to raw resources in resource-managing classes. 74 Item 16: Use the same form in corresponding uses of new and delete. 78 Item 17: Store newed objects in smart pointers in standalone statements. 80 Chapter 4: Designs and Declarations(新增批注共28条) 83 Item 18: Make interfaces easy to use correctly and hard to use incorrectly. 83 Item 19: Treat class design as type design. 89 Item 20: Prefer pass-by-reference-to-const to pass-by-value. 91 Item 21: Don’t try to return a reference when you must return an object. 96 Item 22: Declare data members private. 101 Item 23: Prefer non-member non-friend functions to member functions. 105 Item 24: Declare non-member functions when type conversions should apply to all parameters. 109 Item 25: Consider support for a non-throwing swap. 113 Chapter 5: Implementations(新增批注共42条) 122 Item 26: Postpone variable definitions as long as possible. 122 Item 27: Minimize casting. 125 Item 28: Avoid returning “handles” to object internals. 133 Item 29: Strive for exception-safe code. 137 Item 30: Understand the ins and outs of inlining. 146 Item 31: Minimize compilation dependencies between files. 152 Chapter 6: Inheritance and Object-Oriented Design (新增批注共39条) 162 Item 32: Make sure public inheritance models “is-a.” 163 Item 33: Avoid hiding inherited names. 169 Item 34: Differentiate between inheritance of interface and inheritance of implementation. 174 Item 35: Consider alternatives to virtual functions. 183 Item 36: Never redefine an inherited non-virtual function. 192 Item 37: Never redefine a function’s inherited default parameter value. 194 Item 38: Model “has-a” or is-implemented-in-terms-of” through composition. 198 Item 39: Use private inheritance judiciously. 201 Item 40: Use multiple inheritance judiciously. 207 Chapter 7: Templates and Generic Programming(新增批注共28条) 215 Item 41: Understand implicit interfaces and compile-time polymorphism. 216 Item 42: Understand the two meanings of typename. 220 Item 43: Know how to access names in templatized base classes. 225 Item 44: Factor parameter-independent code out of templates. 230 Item 45: Use member function templates to accept “all compatible types.” 235 Item 46: Define non-member functions inside templates when type conversions are desired. 240 Item 47: Use traits classes for information about types. 245 Item 48: Be aware of template metaprogramming. 251 Chapter 8: Customizing new and delete (新增批注共17条) 258 Item 49: Understand the behavior of the new-handler. 259 Item 50: Understand when it makes sense to replace new and delete. 267 Item 51: Adhere to convention when writing new and delete. 272 Item 52: Write placement delete if you write placement new. 276 Chapter 9: Miscellany(新增批注共8条) 283 Item 53: Pay attention to compiler warnings. 283 Item 54: Familiarize yourself with the standard library, including TR1. 284 Item 55: Familiarize yourself with Boost. 290 Appendix A: Beyond Effective C++ 295 Appendix B: Item Mappings Between Second and Third Editions 299 Index 302 |
随便看 |
|
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。