把《Effective C++》读薄

来源:互联网 发布:java int转short 编辑:程序博客网 时间:2024/04/28 03:35

这本书很早就打算看了,但是一直没有开始。我觉得这本书其实只需要有一定C++经验,越早接触越好!

1

  • view c++ as a federation of languages
    应该看作C、STL、Templete、面对对象四个部分
  • prefer consts,enum,inline to #define
  • use const whenever possible
  • make sure the objects are initialized before they are used;
//这些都是初始化int x=0;const char* a="abcde";double d;cin>>d;Shit(string name,string address):name(name),address(address){}//Shit(stirng name,string address){this.name=name,this.address=address;}会先赋初值

2 Constructor,Destruction,and Assignment Operators

  • know what function C++ silently write and calls
  • Explicitly disallow the use of complier-generated functions you do not want
  • Declare destructors virtual in polymophic base classe
  • Prevent exception from leaving destructors
  • Never call virtual function during construction or destruction
  • Have assignment operator return a reference to *this
  • Handle assignment to self in operator =

3 Resource Management

  • use object to manage resources
  • think carefully about copying behavior in resources manage classed
  • provide access to raw resources on resource-managing classedd
  • use the same form in corresponding use of new and delete
  • store new objects in smart pointers in standalone statements

4 Design and Declarations

  • make interfaces easy to use correctly and hard to use incorrectly
  • treat class design as type design
  • prefer pass-by-reference-to-const to pass-by-value
  • don’t try to return a reference when you must return an object
  • declare date member private
  • prefer non-member non-friend functions to member functions
  • declare non-member functions when type conversions should apply to all parameters
  • consider support for a non-throwing swap
0 0