Executive tive c ++ clause 02, article 02
Use const, enum, and inline instead of # define
I. # define functions
Reference: http://www.crifan.com/_define_detailed/
(1) # define variants, I .e. # ifndef, can prevent repeated references to header files
# Ifdef and # define combinations are generally used in header files to prevent repeated references of multiple files to the same header file. in actual use, even if your header file is not referenced by multiple files, it is best to add it to increase program readability, portability, robustness, and so on. Its usage is generally:
# Ifndef <ID>
# Define <ID>
......... // Include or define.
# Endif
(2) # define variation, that is, # ifdef, which can be added to the desired module (source file)
Add
# Ifdef MYSELF_H
# Include "myself. c"
# Endif
You can add the myself. c code to the source file and add the implemented functions to the myself module.
(3) # define can be used to define macro constants.
# Define PI 3.1415926
# Define RADIUS 5
When expressing the area of the circle, you can use the following representation:
# Define AREA (PI) * (RADIUS ))
Macro definition has some disadvantages:
(1) The type of variables in the macro definition cannot be checked.
(2) Boundary Effect
Ii. const, enum and # define
The variable defined by define is a variable in the Compile-Time period. During compilation, the system replaces all the variables without checking the type and other attributes of the variables, which is relatively unsafe, potential problems may exist, but no problems are found.
Because it is only a replacement during the compilation period, the Defined variables do not allocate memory during runtime and do not occupy memory space.
The variable defined by const is a variable in the Run-Time period. If the type does not match, the system will find and prompt or report an error during running. The corresponding variable const is in the running period, it is also a variable, and the system will allocate memory for it.
Since the macro constants defined by # define are global and cannot be achieved, it is assumed that we should use const to modify data members. The const data member does exist, but its meaning is not what we expected. The const data member is a constant only within the lifetime of an object, but it is variable for the entire class, because the class can create multiple objects, the values of the const data members of different objects can be different.
The const data member cannot be initialized in the class declaration. The following usage is incorrect because the compiler does not know what the SIZE value is when the class object is not created.
Class
{
Const int SIZE = 100; // error, attempted to initialize the const data member in the class declaration
Int array [SIZE]; // error, unknown SIZE
};
The const data member initialization can only be performed in the initialization table of the class constructor, for example
** Variables can be initialized in the constructor's function body.
Class
{
A (int size); // Constructor
Const int SIZE;
};
A: A (int size): SIZE (size) // Of the constructor
{
}
A a (100); // the SIZE of object a is 100
A B (200); // the SIZE of object B is 200
How can we create constants that are constant throughout the class? The following two methods are described:
1. Use enumeration constants in the class. For example
Class
{
Enum {SIZE1 = 100, SIZE2 = 200}; // enumerated constant
Int array1 [SIZE1];
Int array2 [SIZE2];
};
Enumerated constants do not occupy the storage space of objects. They are fully evaluated during compilation. The disadvantage of an enumerated constant is that its implicit data type is an integer, its maximum value is limited, and it cannot represent a floating point number (such as PI = 3.14159 ).
2. Use the keyword static:
Class
{
Static const int SIZE = 100;
Int array [SIZE];
}
This creates a constant named SIZE, which is stored together with other static variables rather than in an object. Therefore, this constant will be shared by all objects in the class.
3. inline and # define
Use inline whenever possible, not explained