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.
new
and
delete
.const
instead of
#define
at global and file scope.Derived[]
as a pointer to Base[]
.new
and
delete
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).new
expression
in a ctor is stored in a dumb pointer class member, make sure
delete
is called on that member in the dtor.operator new
and operator delete
.virtual
in base
classes.operator=
return a reference to *this
. (Note: this says nothing about
declarations).operator=
.operator=
from
a derived class operator=
.virtual
functions in constructors or
destructors.+-/*
when a class has a converting ctor.public
data members.const
instead of
pass-by-value where both are valid and the former is likely to be more
efficient.+-/*
return
an object, not a reference.const
.int
.virtual
inheritance, i.e., make sure there are at least two inheritance paths to
each virtual
base class.const
.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]....
" in function parameter lists.virtual
function.explicit
single-argument ctors and implicit type
conversion operators).&&
,
||
, or ,
.operator++
and
operator--
have the correct return type.++
and --
when the result of the increment or decrement expression is unused.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.[ Miscellaneous | Krishna Kunchithapadam ]
Last updated: Sun Jun 27 17:00:19 PDT 2004
URL: http://geocities.datacellar.net/krishna_kunchith/misc/rules.html