Original article link
Try to use const and inline instead of # define
It is also called: Try to use the compiler instead of preprocessing.
Cause:
(1) # Define aspect_ratio 1.653Pre-compileProgramAspect_ratioReplace 1.653When a compilation error occurs, only the 1.653But aspect_ratio cannot be tracked., Not easy to debug
(2) # Define max (A, B) (a)> (B )? (A): (B ))
Possible errors:
Int A = 5, B = 0;
Max (++ a, B); //The value is increased by 2.Times
Max (++ A, B + 10); //The value is increased by 1.Times
Solution:
(1)Use constInstead of # define:
Const double aspect_ratio = 1.653;
(2) Using inlineInstead of # define:
Inline int max (int A, int B) {return A> B? A: B ;}
Inline functions provide both macro efficiency and predictable behavior and type security.
To use multiple types of templates :,
Template <class T>
Inline const T & MAX (const T & A, const T & B)
{Return A> B? A: B ;}
----------------------------------------------------- Note :--------------------------------------------------------
Usage of constants in the class:
(1) To restrict a constant to a class, you must first make it a member of the class. To ensure that a constant has only one copy at most, you must define it as a static member:
Class gameplayer {
PRIVATE:
Static const int num_turns = 5; // constant eclaration
Int scores [num_turns]; // use of constant
...
};
The preceding statement is a num_turns statement, not a definition, so it must be implemented in the class.CodeStatic members of the class defined in the file:
Const int gameplayer: num_turns; // mandatory definition;
// Goes in class impl. File
(2) The old compiler will not accept this syntax because it considers the static member of the class to be illegal to define the initial value during Declaration; and, only integer types (such as int, bool, and char) can be initialized in the class, and can only be constants.
When the preceding syntax is not available, you can assign an initial value during definition:
Class engineeringconstants {// This goes in the class
PRIVATE: // header file
Static const double fudge_factor;
...
};
// This goes in the class implementation file
Const double engineeringconstants: fudge_factor = 1.35;
(3) but when the class is compiled, the constant of this class is used. For example, the above gameplayer: Scores array declaration (the compiler must know the size of the array during compilation ), to make up for the deficiencies in the compilers that (incorrectly) prohibit class constant initialization, you can use the method called "borrow Enum" to solve the problem. This technique makes good use of the enumeration type principle when int type is required, so gameplayer can also be defined as follows:
Class gameplayer {
PRIVATE:
Enum {num_turns = 5} // "The Enum hack"-makes
// Num_turns a symbolic name
// For 5
Int scores [num_turns]; // fine
};
Unless you are using an old Compiler (that is, before 1995), you do not have to borrow enum.
In combination with the above situation, to be compatible with the old compiler, and to determine the size of the array in the class, constants can be used in the following order:
Consider using Enum first;
Declare static constants when defining classes and initialize them when implementing classes;
If the compiler permits, You can initialize static constants when defining classes.
--------------------------------------------------------- End -------------------------------------------------------------