Wednesday, June 2, 2010

More Effective C++

More Effective C++

    Basics
  1. Pointers and References.
    - A reference must be initialized. There is no null reference.
    - result of the following is undefined:
    char *pc = 0; // set pointer to null
    char& rc = *pc; // make reference refer to
    - Reference is more efficient, b/c there is no need to test its validity.
    - Pointers should generally be tested against NULL first.
    - A pointer can be reassigned, but a reference does not change.
    - certain operators need to use reference, e.g., [].

  2. Prefer C++-style casts. (over C-style cast: (type) expression)
    - static_cast (similar to C-style cast in fxn): static_cast(expression)
    - const_cast: cast away the constness or volatileness of an expression. enforced by compiler
    - dynamic_cast: perform safe casts down or across an inheritance hierarchy. Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references).
    - reinterpret_cast: result is implementation-defined. rarely portable.

  3. Never treat arrays polymorphically.
    - not able to distinguish between base type and derived type for correct polymorphism.

  4. Avoid gratuitous default constructors.

  5. Operators
  6. Be wary of user-defined conversion functions.
    - Single-argument constructors
    - Implicit type conversion operators (better avoid)

  7. Distinguish between prefix and postfix forms of increment and decrement operators.
    - E.g. Class UPInt. prefix ++ returns reference, postfix ++ returns const object.
    - i ++++ is inhibited.
    - i ++ is less efficient because it creates a temporary copy of its return value.

  8. Never overload &&, ||, or ,.
    - otherwise it'll lose short-circuit semantics, and sequence of evaluation becomes uncertain.

  9. Understand the different meanings of new and delete.
    - new operator: 1) allocates memory, 2) calls constructor
    - operator new: does memory allocation only. knows nothing about constructor
    - placement new
    - Deletion and Memory Deallocation
    - Array

  10. Exceptions
  11. Use destructors to prevent resource leaks.
    - pointer operation may lead to memory leak if exception is thrown
    - a possible solution is to use local object instead of pointer, with help of smart_ptr, or STL auto_ptr.

  12. Prevent resource leaks in constructors.
    - destructor deletes only FULLY constructed objects. So if an exception is thrown in constructor, destructor won't be called.

  13. Prevent exceptions from leaving destructors.
    - if control leaves a destructor due to an exception while another exception is active, C++ terminates the program.
    - stack unwinding.

  14. Understand how throwing an exception differs from passing a parameter or calling a virtual function. (more reading needed)

  15. Catch exceptions by reference.
    - four standard exceptions: 1) bad_alloc (thrown when operator new (see Item 8) can't satisfy a memory request), 2) bad_cast (thrown when a dynamic_cast to a reference fails; see Item 2), 3) bad_typeid (thrown when dynamic_cast is applied to a null pointer), and 4) bad_exception (available for unexpected exceptions).

  16. Use exception specifications judiciously. (more reading needed)

  17. Understand the costs of exception handling.

  18. Efficiency
  19. Remember the 80-20 rule.

  20. Consider using lazy evaluation. (more reading needed)

  21. Amortize the cost of expected computations.
    - over eager evaluation, caching
    - prefetching

  22. Understand the origin of temporary objects.

  23. Facilitate the return value optimization.

  24. Overload to avoid implicit type conversions.

  25. Consider using op= instead of stand-alone op.

  26. Consider alternative libraries.

  27. Understand the costs of virtual functions, multiple inheritance, virtual base classes, and RTTI.
    - virtual table (vtable)
    - RTTI: runtime type identification

  28. Techniques
  29. Virtualizing constructors and non-member functions.

  30. Limiting the number of objects of a class.

  31. Requiring or prohibiting heap-based objects.

  32. Smart pointers.

  33. Reference counting.
    - a simple form of garbage collection.

  34. Proxy classes.

  35. Making functions virtual with respect to more than one object.

  36. Miscellany
  37. Program in the future tense.

  38. Make non-leaf classes abstract.

  39. Understand how to combine C++ and C in the same program.
    - name mangling
    - initialization of statics
    - dynamic memory allocation
    - data structure compatibility

  40. Familiarize yourself with the language standard.

  41. Recommended Reading
    An auto_ptr Implementation

No comments:

Blog Archive

Followers