美·Scott Meyers 候捷 电子工业 2011


刚才看到个会议时间有点晚,3.25论文都提交了

谷歌去广告的插件,

最后投了这个会议,刚刚好正合适。我说金钱与时间


ACCUSTOMING YOURSELF TO CPP 基本须知


1, view c++ as a federation of languages.

c: no templates,no exceptions,no overloading

object c++: c with classes, encapsulation , inheritance,polymorphism,virtual(dynamic resistance

template c++:泛型编程 , meta programming,

stl: containers,iterators,algorithms,function

2, Prefer consts,enums,and inlines to #defines.

enum more like define than const, access address define/enume is invalid.

形式函数的宏用inline:

3,use const whenever possible.

mutable 变量总是可被更改,即使在const 函数内

4,Make sure that objects are initialized before they’re used;

成员初始序列比赋值 更高效。

singleton 的一个常见手法,non-local static –> local static 返回一个local static 指针,可避免初始化的无序情况。


CONSTRUCTORS DESTRUCTORS AND ASSIGNMENT 标准的代码方法


5,Know what functions Cpp silently writes and calls.

6,Explicitly disallow the use of compiler generated functions you do not want.

将成员函数声明为private 而故意不实现

私有继承基类的private

7,Declare destructors virtual in polymorphic base classes.

如果class 不带虚,表示它并不想做基类。

虚析构适用于有多态性质的基类,通过基类接口处理子类对象。

8,Prevent exception from leaving destructors.

析构绝对不要吐异常,析构应该能捕捉任何异常,吞下任何异常

要对异常做反应,应提供一个普通函数

9,Never call virtual functions during construction or desctruction.

因为构造和析构期间调用的virtual从不下降至derived

10,Have assignment operators return a reference to *this.

为了实现链式赋值,操作符必须返回一个reference: *this

11,Handle assignment to self in operator=

identity test.

将原来的用局部副本暂存,最后删除。

copy and swap

12,copy all parts of an object.

确保复杂所有base class 成份

不要在构造内调用拷贝或相反的,应使用第三个函数建立共同机制


RESOURCE MANAGEMENT 解决内存泄漏


13,Use objects to manage resources.

RAII对象 获得资源迅速放进管理对象 Resource acquisition is Initialization;RAII

两个常用的RAII类,管理对象运用析构函数确保资源被释放。

智能指针auto_ptr 复制动作会使被复制对象指向NULL

引用计数RCSP copy行为比较直观

用在基于堆的对象上,析构内做delete

14,Think carefully about copying behavior in resource-managing classes

资源管理类中小心copying行为,提制RAII对象必须复制它们的资源,所以资源的copying行为决定RAII的成败。

常用一:抵制复制,copying声明为private

常用二:底层使用引用计数

深拷贝,复制底部资源

转移底部资源拥有权

15,Provide access to raw resources in resource-managing classes.

在资源管理类中提供对原始资源的访问

显示转换安全,隐式转换方便。

16,Use the same form in corresponding uses of new and delete.

new[] 和 delete[]

17,Store newed objets in smart pointers in standalone statements.

以独立语句将new的对象转入智能指针


Designs and Declarations 设计与声明


18,Make interfaces easy to use correctly and hard to use incorrectly

让接口容易被使用, 不易被误用

int fib1(int n){
    int a =0,c;
    for(int b =1 ,i =2; i<= n; i++){ 
      c = a + b;   a =b;    b = c; 
      }
    return c;
   }

explicit 防止隐式转换的

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.普通构造函数能够被隐式调用。而explicit构造函数只能被显示调用。

引用计数 支持定制型删除器,防止跨DLL问题,自动解除互锁

19,Treat class design as type design 设计class 就像设计type

对象创建和销毁

对象初始化和对象赋值区别

20,Prefer pass by reference to const  to pass by value

规则不适合内置类型/STL迭代器和函数对象。

简单地说,函数对象就是一个重载了()运算符的对象,它可以像一个函数一样使用。

21,Don't try to return a reference when you must return an object.

不要返回指向局部栈的对象,或者需要多个局部静态对象

22,Declare data mebmbers private

将成员变量声明为private.

23,Prefer non-member non-friend functions to member functions.

增加封闭性和扩充性

24,Declare non-member functions when type convrsons should apply to all parameters.

要为某个函数的所有参数进行类型转换,必须是非成员函数。

25,Consider support for a non-throing swap.

如果提供一个成员函数,也应该提供一个非成员函数调用前者,调用swap时使用using



IMPLEMETATIONS


26,Postpone variable definitions as long as possible.

27,Minimizing casting

避免dynamic_cast

转型隐藏于函数之后,

使用C++式转型

28,Avoid returning handles to object internal.

29,Strive for exception safe code.

30,Understand the ins and outs of inlining.

大多数是编译期行为

31,文件间的编译依存降至最低

依赖声明式,不依赖定义式



INHERITANCE AND OOD


32,确定public 是is a 继承

33,Avoid hiding inherited names.

34,Differentiate between inheritance of interface and inheritance of implementation.

35,考虑虚函数外的其它选择

设计模式

36,绝不重新定义继承过来的非虚函数

37,Never redefine a function inherited default parameterv alue.

虚函数是动态绑定,缺省参数是静态绑定,

38,复合出has a 或者根据某物实现

39,Use private ihneritance judiciously

尽可能复合,必要才private继承

40,Use multiple inheritance judiciously.



TEMPLATE AND GENERIC


41,Understand implicit interfaces and compile timpe polymorphism.

以不同的template参数具体化函数模板,会导致调用不同的函数,这是所谓编译多态。哪一个函数被重载调用。

虚函数绑定是运行期

42,Understand the two meanings of typename.

43,Know how to access names in templatized base classes.

44,Factor parameter independent code out of templates.

45,Use member function templates to accept all compatible types.

46,Define non member functions inside templates when type conversions are desired.

47,请使用traits class 表现类型信息,使类型信息在编译期可用

48,Be aware of template meta programming



NEW AND DELETE


49,Understand the behavior of the new handler.

50,Understand when it makes sense to replace new and delete.

51,Adhere to convention when writing new and delete.

52,placement new and placement delete



MISCELLANY


53,Pay attention to compiler warnings

54,Familiarize yourself with the standard libray, including TR1

不熟悉TR1却奢望成为C++高手 是不可能的。

55,让自己熟悉Boost


我昨天泡在图书馆一天把这本书解决掉了。虽然因为手机关机连过去多少时间都不知道那时还没意识到中午饭都没吃倒也还是挺高兴的,

我觉得图书馆看书的好处是很快,坏处是记不得。结论是好书读一遍是不够的。

转载于:https://www.cnblogs.com/iamgoodman/p/3474234.html

Effective C++ 改善55个方法相关推荐

  1. 计算机机表格日生产量怎么算,如何学习工业工程的改善思维及方法

    原标题:如何学习工业工程的改善思维及方法 ☞这是金属加工(mw1950pub)发布的第11851篇文章 编者按 由于综合环境变得越来越复杂,现代实体制造业受到种种冲击.尤其是随着国内人口红利的消失,企 ...

  2. 详解:生产线平衡改善的四大方法与八大步骤!

    人类的一切形态都是追求高生产效率和效益的目标.通过增强生产线的平衡性是提高生产能力的有效思路,是流水线生产企业追求的目标.我们经常性可以听到一条龙生产,就涉及到整个生产的各个环节情况.生产线平衡改善不 ...

  3. Effective C++改善程序与设计的55个具体做法笔记

    Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...

  4. 不为人知的 35 个 More Effective C++ 改善编程与设计的最佳方法 | 原力计划

    作者 | fengbingchun 责编 | 屠敏 出品 | CSDN 博客 Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.E ...

  5. (6)继承与面向对象设计- Effective C++改善程序与设计的55个具体做法(Effective C++: 55 Specific Ways to Improve Your Programs)

    文章目录 32. 确定你的public继承塑模出is-a关系(Make sure public inheritance models "is-a") 33. 避免遮挡继承而来的名称 ...

  6. 《Effective C++:55个提升性能的办法》阅读笔记

    一 然自己习惯C++ 1 视C++为一个语言联邦   C++语言本身的出身和目标和其名称表达的意思相近,作为C语言的超集.C++的最初的目标是在保证对C的完全兼容的前提下扩充面向对象的能力,提升研发效 ...

  7. Effective Java (7) - 避免终止方法

    一. 基本概念 1. 所谓的终结方法事实上是指finalize(). 2. Java的垃圾回收机制仅仅负责内存相关清理.其它资源的清理(释放文件.释放DB连接)须要程序猿手动完毕. 3. 调用Syst ...

  8. 精益管理学会|什么是ECRS改善方法?

    ECRS是IE工程改善.精益生產管理改善的四大法宝. 针对现有的生产线进行改善时,常见的做法是对现有的生产线进行绘制各工站的工时山积表如下圖所見,然后对各工站的动作单元进行ECRS 改善. E:不需要 ...

  9. 矩阵奇异性和“病态”问题的解释与改善方法(简单易懂)

    文章目录 矩阵奇异性.'病态'问题描述: 常用改善条件数的方法: 关于条件数和正则化的概念补充: 矩阵奇异性.'病态'问题描述:   在实际工程应用中,求解线性方程组 AX=BAX=BAX=B 问题时 ...

最新文章

  1. 基数字符串排序c语言,基数排序(C语言)
  2. Python语言学习之字母A开头函数使用集锦:assert用法之详细攻略
  3. 【Android】【转】查看内存
  4. [SpringBoot2]yaml
  5. hdu 2112 ——HDU Today
  6. 中webgl解析json_WebGL蒙皮(下)
  7. Python字典values()方法与示例
  8. AdvancedEAST高效场景文本检测(附Github地址)
  9. 有图有真相:带你实现当下流行的权限验证
  10. java存储数据_Java 数据存储
  11. 一个程序掌握C++带参构造函数、带有默认参数的构造函数【C++类的经典使用案例】
  12. 负离子程序员的一组未来手绘,酷毙了
  13. python简单代码表白-表白python代码
  14. 图片和视频的相互转换
  15. 黑帽SEO网站优化常用的15种技巧
  16. 托福高频真词List09 // 附托福TPO阅读真题
  17. 畜牧养殖物联网的应用功能
  18. 一、在PyCharm上直接调试py脚本
  19. VBA之正则表达式(19)-- 相对引用转绝对引用
  20. 树莓派魔镜——MagicMirror使用(一):开启MagicMirror

热门文章

  1. vscode 插件使用(前端力推)
  2. bootstrap,datetimepicker日期时间选择器-限制时间段,以及中文显示问题
  3. 开源的酷炫猜歌喝酒小程序
  4. Let导航网系统源码系统+一键收录
  5. WordPress主题 RiPro v5.0高级付费素材资源类主题
  6. smartassembly 使用指南
  7. 在Nginx上配置NameCheap免费SSL
  8. Memcache 中实现消息队列
  9. Linux——批量查找替换方法(VIM和sed)
  10. C/C++——有关转义字符和ASCII码表