Clause 02: replace const, Enum, and inline as much as possible # define

Source: Internet
Author: User

What do we often do with define?

1) define a constant

2) define a macro

Why do not use define. The following two aspects are explained:

1) when we define a constant, we can use the following method:

# Define aspect_patio 1.653

We know that the compiler does not see the aspect_patio symbol (why? ReviewProgram). So this mark aspect_patio is not included in the compiler's mark table. If we get an error in the compilation period, this error message will mention 1.653 instead of aspect_patio. At this time, you will be puzzled.

If the aspect_patio symbol is not defined in this file, you may be confused.

The reason for both cases is that the name you use may not be in the mark table.

 

What should I do?

We can use a const constant to replace the macro above.

Const double asspectration = 1.653; as a language constant, it is seen by the compiler and recorded in the symbol table.

 

Another advantage of using const to replace define is that you can define the exclusive constants of a class. However, define has no effect on the category.

For example, we use const to define the exclusive constants of a class:

Class gameplayer {

Static const int numturns = 5; // This is a declaration. Because it is an integer constant, you can give an initial value in the class definition.

}

Const int gameplayer: numturns; // This is the definition of numturns. Since the initial value is previously given, no value is assigned here.

If you use define to define a constant, it not only belongs to this class, but also belongs to other classes. No encapsulation.

2) define a macro

Why macro is used, because the macro looks like a function, but does not incur additional overhead during function calling.

For example, define a macro with the maximum two values.

# Define max (A, B) (a)> = (B )? (A): (B ))

Note that parentheses must be added to all real parameters in the macro. Otherwise, the problem may occur.

 

However, we can use inline to eliminate these risks.

Template <class T>

Inline T max (const T & A, const T & B ){

Return (A> = B? A: B );

}

Benefits of using inline: the compiler canCode.

 

Ask if define is useless?

No. With const and inline, our demand for pre-compiler (especially define) is reduced. But it is not eliminated. # Include is a necessity, while # ifdef, # ifndef continues to play the role of controlling compilation.

 

 

Remember:

    • For simple constants, it is best to replace # define with a const object or enum.
    • For macros like functions, it is best to replace # defines with the inline function.

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.