Ilocker: Focus on Android Security (novice) qq:2597294287
1 #define Aspect_ratio 1.653
The token aspect_ratio does not enter the symbol table because it is replaced in the preprocessing phase.
If you get a compilation error when you apply this constant, and the macro definition is not what you write, you will be confused about what 1.653 is and where it comes from. As a result, we are wasting time by tracking code.
should be replaced with constants:
1 Const Double 1.653;
This may also reduce the volume of the object code, because the preprocessor "blindly" macro substitution, can result in multiple copies of 1.653.
If you want to define a constant char * string, then:
1 Const Char const kauthorname = "Scott Meyers";
Two const, one constraint string content is not mutable, one constraint string pointer is immutable.
or this:
1 CONST std::string kauthorname ("Scott Meyers");
In addition, macros do not have scope restrictions and cannot provide any encapsulation, but const constants can.
1 class Gameplayer {2private:3 Static Const int 5 ; 4 int Scores[knumturns]; 5 }
Then in the definition file:
1 Const int Gameplayer::knumturns;
If the knumturns is not used in the array declaration, the setting of its initial value can also be placed in the definition.
If the compiler does not support In-class initial setting, you can also use the so-called "enum hack" technique:
1 class Gameplayer {2private:3 enum 5 }; 4 int Scores[knumturns]; 5 }
Perhaps the enum hack is better for this kind of application. Enum hack behaves more like a #define, because an enum value cannot be taken as an address.
Another common use of #define is to define a "pseudo function", which does not incur the additional overhead associated with function call.
1 #define Call_with_max (A, B) f ((a) > (b)? (a): (b))
It is very careful to define such a macro, with parentheses in each argument, but not in the following cases:
1 // A may be accumulated two times
In this case, you can substitute the template inline function:
1 template<typename t>2void Callwithmax (constconst T &B ) {3 F (a > B? a:b); 4 }
Finally, #define may only be used to prevent duplicate include for header files.
Learning materials: "Effective C + +"
C + + Note (i)-try to replace #define with Const, enum, and inline