Item 2:prefer consts, enums, and Inlines to #defines
Try to use constants, enumerations and inline functions instead of #define . We know #define the macros defined by are replaced at compile time, and are part of the concept of modular programming. Macros are global, and the encapsulation is broken in object-oriented programming. So try to avoid it in C + +!
Then we look specifically #define at the problems that are causing the problem.
Not easy to understand
It is well known that the macro definition is best enclosed in parentheses because of the reason that the preprocessor is replaced directly. The #define function will produce unexpected results:
#define MAX (A, b) a> B?a:b max ( i ++ j " Span class= "P" style= "margin-bottom:0px" >)
iThe number of times the self-addition will depend on j the size, however the caller does not know. Macro behavior is not easy to understand, essentially because the macro is not part of the C + + language, it is only the source code preprocessing means.
Not conducive to commissioning
Macro substitution occurs at compile time before the grammar check. Therefore, the macro name does not appear in the related compilation error, and we do not know which macro is out of the question. For example:
#define PERSON alicePERSON=bob;
IfaliceNot defined,PERSON=bob;Error will occur:use of undeclared identifier ‘alice‘。 But we may not knowaliceWhat is it,PERSONis the "variable" that we define.
macro substitution is done during preprocessing, in principle the compiler does not know the concept of macros. However, in modern compilers (e.g. apple LLVM version 6.0 ), the compiler records the macro substitution information and gives the name of the macro in the compilation error:
test.cpp:8:5:error:use of undeclared identifier ' alice ' person = Bob; ^test.cpp:4:16:note:expanded from the macro ' person ' #define the person Alice; ^
As a result, the problem mentioned by Meyers no longer exists. The author's intention, however, is to use the compiler instead of the preprocessor as much as possible. Because it #define 's not part of the C + + language.
Enum is more useful than const
#define cannot be encapsulated in a class, we can use static const defines a constant and acts on it in the current class:
class C{ static const int NUM = 3; int a[NUM];};
Typically C + + requires all declarations to be defined, whereas numeric types (char,int,longThe static constants can be given only to declarations. Here'sNUMis an example. However, if you want to takeNUMAddress, you get a compile error:undefined symbol NUM。 To do this, you usually need to give a definition at the same time:
class C{ Static Const int NUM = 3; int a[NUM];};Const int C::NUM;
Because NUM the initial value was given at the time of declaration, it is not allowed to give the initial value again when defined.
If using enum , things will be much simpler:
class c enum { num = 3 }; int a [ num ]; };
Unless noted, this blog article is original, reproduced please link to the form of this article address: http://harttle.com/2015/07/20/effective-cpp-2.html
Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link.
Item 2: Avoid using define effective C + + notes