12.24

Effective C++ Scott Meyers  

Chapter 2. Constructors, Destructors, and Assignment Operators

1. Item 5: Know what functions C++ silently writes and calls.

  The generated destructor is non-virtual unless it's for a class inheriting from a base class that itself declares a virtual destructor.

  C++ doesn't provide a way to make a reference refer to a different object.

  If you want to support copy assignment in a class containing a reference member or a const member, you must define the copy assignment operator yourself.

  Compilers reject implicit copy assignment operators in derived classes that inherit from base classes declaring the copy assignment operator "private".

2. Item 6: Explicitly disallow the use of compiler-generated functions you do not want.

  It's alwasy good to have earlier error detection, so move the link-time error to compile-time error is good.

  Using a base class like Uncopyable is a way to disallow functionality automatically provided by compilers.

3. Item 7: Declare destructors virtual in polymorphic base classes.

  Any class with virtual functions should almost certainly have a virtual destructor.

  When a class is not intended to be a base class, making the destructor virtual is usually a bad idea.

  Each class with virtual functions has an associated virtual table.

  If the class contains a virtual function, objects of that type will increase in size.

  If you're ever tempted to inherit from a standard container or any other class with a non-virtual destructor, resist the temptation.

  Occationally, it can be convenient to give a class a pure virtual destructor.

4. Item 8: Prevent exceptions from leaving destructors.

  Two ways to deal with exception possible code in destructors:

    1. Terminate the program. Calling abort in catch block.

    2. Swallow the exception.

  A better approach is to provide an interface to client to let them handle the exception.

  Destructors should never emit exceptions. If functions called in a destructor may throw, the destructor should catch any exceptions, then swallow them or terminate the program.

4. Item 9: Never call virtual functions during construction or destruction.

  Using a helper function to create a value to pass to a base class constructor is often more convenient than going through contortions in the member initialization list to give the base class what it needs.

5. Item 10: Have assignment operators return a reference to *this.

6. Item 11: Handle assignment to self in operator=.

  Make sure operator= is well-behaved when an object is assigned to itself. Techniques include comparing addresses of source and target objects, careful statement ordering, and copy-and-swap.

  Make sure that any function operating on more than one object behaves correctly if two or more of the objects are the same.

7. Item 12: Copy all parts of an object.

  When you're writing a copying function, be sure to (1) copy all local data members and (2) invoke the appropriate copying function in all base classes, too.

  If you find that your copy constructor and copy assignment operator have similar code bodies, eliminate the duplication by creating a thrid member function that both call.

 

Chapter 3. Resource Management

8. Item 13: Use objects to manage resources.

9. Item 14: Think carefully about copying behavior in resource-managing classes.

  Copying an RAII object entails copying the resource it manages, so the copying behavior of the resource determines the copying behavior of the RAII object.

  Common RAII class copying behaviors are disallowing copying and performing reference counting, but other behaviors are possible.

10. Item 15: Provide access to raw resources in resource-managing classes.

11. Item 16: Use the same form in corresponding uses of new and delete.

  If you use [] in a new expression, you must use [] in the corresponding delete expression.

  Abstain from typedefs for array types.

12. Item 17: Store newed objects in smart pointers in standalone statements.

 

Chapter 4. Designs and Declarations

13. Item 18: Make interfaces easy to use correctly and hard to use incorrectly.

  Ideally, if an attempted use of an interface won't do what the client expects, the code won't compile; and if the code does compile, it will do what the client wants.

  Many client errors can be prevented by the introduction of new types.

  Ways to prevent erros include creating new types, restricting operations on types, constraining object values, and eliminating client resource management responsibilities.

14. Item 19: Treat class design as a type design.

15. Item 20: Prefer pass-by-reference-to-const to pass-by-value.

  If you have an object of a built-in type, STL iterator and function object types, it's often more efficient to pass it by value than by reference.

16. Item 21: Don't try to return a reference when you must return an object.

  Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than once such object will be needed.

17. Item 22: Declare data members private.

  Protected is no more encapsulated than public.

18. Item 23: Prefer non-member non-friend functions to member functions.

  Prefer non-member non-friend functions to member functions. Doing so increases encapsulation, packaging flexibility, and functional extensibility.

19. Item 24: Declare non-member functions when type conversions should apply to all parameters.

  If you need type conversions on all parameters to a function, the function must be a non-member.

  

  

   

  

  

posted @ 2018-12-24 13:57  lefthook  阅读(114)  评论(0编辑  收藏  举报