zoukankan      html  css  js  c++  java
  • Effective C++ 改善55个方法

    美·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


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

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

  • 相关阅读:
    合并ts文件
    Typora
    Typora
    OCMock 3 参考
    git 修改上次提交信息 与 撤销此操作.
    git使用技巧
    python获取软件安装列表2222
    【Dojo 1.x】笔记6 配置对象dojoConfig的用处和真身所在
    【Dojo 1.x】笔记目录
    【Dojo 1.x】笔记5 使用本地引用
  • 原文地址:https://www.cnblogs.com/iamgoodman/p/3474234.html
Copyright © 2011-2022 走看看