English
[Effective C++] item 02

[Effective C++] item 02

To write #define, think of const, enum, and inline.

#Prefer Const, Enum, and Inline over #define

  • Expresses preference for compiler over preprocessing

#define FOO 1.024

  • The above case is a preprocessor that substitutes the string before compilation.

  • The above symbol FOO can be removed without the compiler being aware of its existence, so it may appear as a constant in errors, so if there is a bug related to this, it will be a waste of time to track it down.

const double FOO = 1.024;

  • The solution is to replace the macro with a constant.

  • The constant is clearly observable by the compiler and is also clearly entered in the symbol table. Also, if the substitution is done in bulk through a preprocessor, multiple copies of the object may be created, but in the case of a constant, no more than one copy is created.

댓글 작성

게시글에 대한 의견을 남겨 주세요.

댓글 0