C + + Note (i)-try to replace #define with Const, enum, and inline

Source: Internet
Author: User

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

Related Article

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.