1. #define为预处理阶段命令
Cause: It is possible that the token name did not enter the token table and a compilation error occurred, that is, the compiler did not see the definition.
Class-specific const constants are generally defined as static, guaranteeing that the constant has at most one copy of the entity.
The enumeration type value can act as a ints type use. ENUM{NUM=3}; Now num is a mark of 3.
For FETCH address operations: const is legal, but enum and define cannot take address operations.
#define定义函数宏的时候容易产生歧义. (parentheses are assigned, and so on, in the Order of calculation: )
Macros cannot be defined as private, but inline functions inline can be defined as private inline.
Summary: For simple constants, do a const,enum instead of # define
For a macro definition of a function type, it is best to use inline instead, because inline functions are well-respected for scopes and access rules.
2. Try to use const when defining invariant constants. The compiler helps enforce this constraint. Const can be used to modify: Constants (global, local), functions, files, static objects in a chunk scope, pointers, etc...
Const modifier pointer: const on the left, indicating that the object is a constant, and the const appears on the right, indicating that the pointer itself is a constant. Can also appear on both sides.
Left: const char*ptr; equivalent to char const*ptr; constant pointer, pointer to constant.
Right: The pointer constant, the point of the thing can not change, but the value of the thing is variable.
Constant const modifier function: A function return value is a constant that can prevent errors caused by user actions.
Const member function: makes it easy to call a const variable. Two functions with different constants can be overloaded, that is, if there are two functions that have const adornments, they are different, similar to overloaded operations.
Const char& operator[] (size_t p) const{...}
char& operator[] (size_t p2) {...}
Overloaded with two different functions:
Note: When Const has an essentially the same implementation as the NON-CONST member function (there are some repetitive operations), using NON-CONST to invoke the const version of the function avoids code duplication. The reason is that the Const promise does not change the logical state of the object, but when you use the const to invoke the non-const, the non-const does not promise not to change the logical state of the object, which creates an error.
1. Replace define with const, enum,inline as possible