Effective C++

This is a summary of the items from the books Effective C++ and More Effective C++ by Scott Meyers.
The intended use is for the software architect to select the items to require, emphasize or ignore for their project - and for the programmers to refer to the book for explanations.
Shifting from C to C++
  • Prefer const and inline to #define
  • Prefer to
  • Prefer new and delete to malloc and free
  • Prefer C++ style comments
Memory Management
  • Use the same form in corresponding uses of new and delete
  • Use delete on pointer members and destructors
  • Be prepared for out-of-memory conditions
  • Adhere to convention when writing operator new and operator delete
  • Avoid hiding the “normal” form of new
  • Write operator delete if you write operator new
Constructors, Destructors and Assignment Operators
  • Declare a copy constructor and an assignment operator for classes with dynamically allocated memory
  • Prefer initialization to assignment in constructors
  • List members in an initialization list in the order in which they are declared
  • Make destructors virtual in base classes
  • Have operator= return a reference to *this
  • Assign all data members in operator=
  • Check for assignment to self in operator=
Classes and Functions: Design and Declaration
  • Strive for class interfaces that are complete and minimal
  • Differentiate among member functions, non-member functions and friend functions
  • Avoid data members in the public interface
  • Use const whenever possible
  • Prefer pass-by-reference to pass-by-value
  • Don't try to return a reference when you must return an object
  • Choose carefully between function overloading and parameter defaulting
  • Avoid overloading on a pointer and a numerical type
  • Guard against abiguity
  • Explicitly disallow use of implicitly generated member functions you don't want
  • Partition the global namespace
Classes and Functions: Implemention
  • Avoid returning “handles” to internal data
  • Avoid member functions that return non-const pointer or references to members less accessible than themselves
  • Never return a reference to a local object or to a de-reference pointer initialized by new within the function
  • Postpone variable definitions as long as possible
  • Use in-lining judiciously
  • Minimize compilation dependencies between files
Inheritance and Object-Oriented Design
  • Make sure public inheritance models “isa”
  • Differentiate betwen interitance of interface and inreitance of implementation
  • Never redefine an inherited nonvirtual function
  • Never redefine an inherited default parameter value
  • Avoid casts down the inheritance hierarchy
  • Model “has” or “is-implemented-in-terms-of” through layering
  • Differentiate between inheritance and templates
  • Use private inheritance judiciously
  • Use multiple inheritance jusdiciously
  • Say what you mean; understand what you are saying
  • Know what functions C++ silently writes and calls
  • Prefer compile-time and link-time errors to runtime errors
  • Ensure that non-local static objects are initialized before they are used
  • Pay attention to compiler warnings
  • Familiarize yourself with the standard library
  • Improve your understanding of C++