Rules for C++ Programming

The following rules are derived from the A First Look at C++ Program Analyzers paper by Scott Meyers and Martin Klaus. The [Exx] and [Mxx] notes are references to the Effective C++ and More Effective C++ books rspy.

Table of Rule Categories


General

  1. [E1] Use const instead of #define at global and file scope.
  2. [M2] Use new-style casts instead of C-style casts.
  3. [M3] Don't treat a pointer to Derived[] as a pointer to Base[].

Use of new and delete

  1. [E5] Use the same form for calls to new and delete. (In general, this calls for dynamic analysis, but static analysis can catch some special cases, e.g., calls to new in ctors and delete in dtors).
  2. [E6] When the result of a new expression in a ctor is stored in a dumb pointer class member, make sure delete is called on that member in the dtor.
  3. [E9] Avoid hiding the default signature for operator new and operator delete.

Constructors/Destructors/Assignment

  1. Copy-ctor and assignment operator.
    1. [E11] Declare a copy constructor for each class declaring a pointer data member.
    2. [E11] Declare an assignment operator for each class declaring a pointer data member.
  2. [E12] Initialize each class data member via the member initialization list.
  3. [E13] List members in a member initialization list in an order consistent with the order in which they are actually initialized.
  4. [E14] Make destructors virtual in base classes.
  5. [E15] Have the definition of operator= return a reference to *this. (Note: this says nothing about declarations).
  6. Assignment operators and copy-ctors.
    1. [E16] Assign to every local data member inside operator=.
    2. [E1] Call a base class operator= from a derived class operator=.
    3. Use the member initialization list to ensure that a base class copy-ctor is called from a derived class copy-ctor.
  7. Don't call virtual functions in constructors or destructors.

Design

  1. [E19] Use non-member functions for binary operations like +-/* when a class has a converting ctor.
  2. [E20] Avoid public data members.
  3. [E22] Use pass-by-ref-to-const instead of pass-by-value where both are valid and the former is likely to be more efficient.
  4. Operators.
    1. [E23] Have operators like +-/* return an object, not a reference.
    2. [M6] And make those return values const.
  5. [E25] Don't overload on a pointer and an int.
  6. [M33] Make non-leaf classes abstract.
  7. [M24] Avoid gratuitous use of virtual inheritance, i.e., make sure there are at least two inheritance paths to each virtual base class.

Implementation

  1. [E29/E30] Don't return pointers/references to internal data structures unless they are pointers/references-to-const.
  2. [M26] Never define a static variable inside a non-member inline function unless the function is declared extern. [Footnote: In July 1996, changes to the nascent standard for ANSI/ISO C++ obviated the need for this rule, at least on paper. However, the need still exists in practice, because many compilers continue to heed the older rules that can lead to duplicated variables in inline non-member functions].
  3. Avoid use of "..." in function parameter lists.

Inheritance

  1. [E37] Don't redefine an inherited non-virtual function.
  2. [E38] Don't redefine an inherited default parameter value.

Operators

  1. [M5] Avoid use of user-defined conversion operators (i.e., non-explicit single-argument ctors and implicit type conversion operators).
  2. [M7] Don't overload &&, ||, or ,.
  3. [M6] Make sure operator++ and operator-- have the correct return type.
  4. [M6] Use prefix ++ and -- when the result of the increment or decrement expression is unused.
  5. [M22] Declare op= if you declare binary op (e.g., declare += if you declare +, declare -= if you declare -, etc.). One way to satisfy this constraint is by providing a template that yields the appropriate function.

Exceptions

  1. [M11] Prevent exceptions from leaving destructors.
  2. [M13] Catch exceptions by reference.

[ Miscellaneous | Krishna Kunchithapadam ]


Last updated: Sun Jun 27 17:00:19 PDT 2004
URL: http://www.geocities.com/krishna_kunchith/misc/rules.html

Hosted by www.Geocities.ws

1