Migrating from C to C++
- The size of a character constant has gone from sizeof(int)
to sizeof(char).
- An object declared at file scope should be initialized. All
references to that global object in other files must use the
extern keyword and must not be initialized.
- const-qualified objects with external linkage must have
an explicit extern specifier.
- Use an explicit cast when converting a void* to another
pointer type.
- Provide typesafe implementations for operator++ and
operator-- as needed for enumeration types.
- Do not define types in a cast expression, parameter
declaration or sizeof expression.
- Do not use goto or switch statements to bypass
initialization of a local object.
- Do not initialize an array of characters using a string
literal with more characters (including the null terminator) than
the size of array.
- Always declare the prototype before calling a function.
Use f(void) to emphasize that function "f" has no
arguments.
- Do not use a C++ keyword as an identifier.
- Do not use a type outside of the scope in which it was
defined.
- In C++, use only new and delete.
- Avoid writing a C-style comment "/* */" immediately after
the token '/'.
Guidelines for Code Size
- Use declarations of the form "T x(i);" rather than
"T x; x=i;".
- Inline small functions only.
- Use compound assignment operators, such as "+=", in preference
to '+' and '=', to avoid creating and destroying temporary objects
unnecessarily.
- Implement class-specific operators new and
delete as needed to improve the speed and memory
utilization of dynamic memory management.
- Avoid coding which depends on the initialization order of
global objects across translation units.
Guidelines for Speed
- Avoid creating and destroying large arrays of objects during
time-critical processing.
- Avoid declaring objects inside a loop. Declare them before
the loop instead.
Guidelines for ROMable code
- Declare objects to be placed in ROM with: static,
constant initializer, and POD (plain ol' data) types. A POD
type is any of:
- a scalar (arithmetic, enumeration or pointer) type
- a class, struct, or union all of whose data members
are public and of POD types, and with no user-defined
constructor or destructor, no base classes, and no
virtual functions
- an array with elements of POD type