1 //2 //2015-03-04 02/553 //replace #define with const enum inline4 //5 6 //prevents the same physical file from being included multiple times7 #pragmaOnce8 9 // ----------------------------------Ten //1. Use const as much as possible One // ---------------------------------- A - classFoo - { the Public: - foo (); - Private: - + //here is the declarative _count for simple types, class-specific - //variable, you can specify the initial value at the time of declaration, but this is still not a defined formula. + Static Const int_count =0; A }; at - //In most cases it is sufficient to provide only _count declarations, if the compiler does need to - //Definition , the following is the definition, this definition should be placed in the CPP file, - //Otherwise, because the file contains multiple occurrences, it can cause redefinition compilation errors . - //There is a link error even if there is no compilation error. - in //const int Foo::_count; - to + // ---------------------------------- - //2. Enum Compensation Method the // ---------------------------------- * $ //If the legacy compiler does not allow you to specify a value for _count when declaring, you can onlyPanax Notoginseng //The const int foo::_count = 0 is placed in the CPP file. But this - //the value of _count cannot be known at compile time, not as the size of the array, below the //the enum hack way can be compensated as follows: + A classBar the { + Public: - Bar () {} $ $ Private: - enum{COUNT =5}; - int_arr[count]; the }; - Wuyi // ------------------------------------------ the //3. Inline instead of the #define of the function type - // ------------------------------------------ Wu - #defineMax (A, B) ((a) > (b)? (a): (b)) About $ //There is obviously a problem with the above #define when they encounter the self-add + + symbol - //For example Max (a++, a++), the number of executions is related to the size of a. - //using the inline + template can be a good solution, as follows: - ATemplate<typename t> + T Max (t A, T B) the { - returna > B?a:b; $}
"02" replaces #define with const enum inline