Item 2: Prefer consts, enums, and inlines to #defines

Use as less preprocessor as possible (or even better, use as less non-zero globals as possible).

  • You should use constant pointer:
const char * const message = "Hello, World!";
// Or const object
const std::string message("Hello, World!");
  • Class-specific constant can be declared wit value but not defined (only for integral types).
class Foo {
  // You don't need to define SIZE unless you take
  // address from it.
  static const int SIZE = 5;
  int array[SIZE];  // You may use its value.
};
Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.