Try to replace # define with const, enum, and inline
First, #define的功能
Reference: http://www.crifan.com/_define_detailed/
(1) #define的变体, or #ifndef, prevents duplicate references to the head file
#ifdef和 #define组合, typically used in header files, to prevent multiple files from repeating a reference to this same header file. In practice, even if your header file is not referenced by multiple files for the time being, it is best to add readability, portability, robustness, etc. Its usage is generally:
#ifndef < identity >
#define < identity >
...//include or define STH.
#endif
(2) #define的变体, that is #ifdef, can be implemented to join their own needs of the module (source file)
In the source file, add
#ifdef MYSELF_H
#include "MYSELF.C"
#endif
Can be implemented in the source file to add MYSELF.C code, the implementation of the function added, that is, the addition of the myself module
(3) #define可以进行宏定义常量
#define PI 3.1415926
#define RADIUS 5
When expressing the area of the circle, it is possible to use the following expression:
#define Area ((PI) * (RADIUS) * (RADIUS))
The macro definition has some drawbacks:
(1) Unable to type check for variables in macro definition
(2) Boundary effect
Second, const, enum and # define
Define defined variables, is the variable of the Compile-time period, the system at compile time, it is all replaced, but not its variable type property checks, relatively not very safe, there may be potential problems, but not found.
Because it is simply a compile-time replacement, the variable it defines is not allocated memory at runtime and does not occupy memory space.
The variable defined by const is a variable of the run-time period, and if the type does not match, the system will find and prompt or error when running, corresponding, the const variable is a variable at runtime, and the system allocates memory for it.
Because the macro constants defined by # define are global and cannot be achieved, it is thought that the data members should be modified with Const. Const data members do exist, but their meaning is not what we expect. Const data members are constants only for the lifetime of an object and are mutable for the entire class, because a class can create multiple objects, and the values of its const data members can be different for different objects.
You cannot initialize a const data member in a class declaration. The following usage is incorrect, because the compiler does not know what the value of SIZE is when the object of the class is not created.
Class A
{
const int SIZE = 100; Error, attempting to initialize const data member in class declaration
int array[size]; Error, Unknown SIZE
};
The initialization of a const data member can only be done in the initialization table of the class constructor, for example
* * Variables can be initialized in the function body of the constructor function
Class A
{
A (int size); constructor function
const int SIZE;
};
A::A (int size): size (size)//constructor
{
}
A (100); Object A has a SIZE value of 100
A B (200); Object B has a SIZE value of 200
How can you build constants that are constant throughout the class? Two methods are described below:
1. The enumeration constants in the class are implemented. For example
Class A
{
enum {SIZE1 = +, SIZE2 = 200}; Enumeration constants
int ARRAY1[SIZE1];
int array2[size2];
};
Enumeration constants do not consume objects ' storage space, they are evaluated at compile time. The disadvantage of an enumeration constant is that its implied 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 A
{
static const int size=100;
int array[size];
}
This creates a constant named size that will be stored with other static variables instead of being stored in an object. Therefore, this constant will be shared by all objects of the entire class.
Three, inline and # define
Try to use inline, do not explain
Effective C + + clause 02 Collation