"02" replaces #define with const enum inline

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.