Definition and usage of macros in C + + (now replaced by inline functions)

Source: Internet
Author: User

In Noip, macros are often used, so here's a look at the definition and usage of macros in C + +

    1. first Usage--match conditional compilation : #define DEBUG

Defines a identifier called Debug. It should be used in conjunction with #ifdef or #ifndef. Examples are as follows:

#define DEBUG#IFDEF debugvoid Print (int v) {cout << v << Endl;} #elsevoid print (int) {} #endif

  

If the symbol debug is present, then the compiler compiles the above print with the output value, otherwise the compiler compiles the print below and does nothing.

Replace the above #ifdef with #ifndef, then the compiled code is just the opposite of what is said above.

2. The second usage-expression :

#define N  5000

At compile time, the compiler uses a method similar to "Find and replace" to replace N in the code with 5000. If you need to change to an expression, enclose them in parentheses. For example:

#define a   1+2#define b   (1+2) c=a*2; d=b*2;

  

At compile time the above line will become "C=1+2*2; d= (1+2) * *, obviously their values are different.

Also, be aware that there can be no semicolon at the end of an expression (unless you want to).

3. The third use-simple "function":

#define FtoC (A) (((a)-32)/9*5)

This is similar to a function. However, because the compiler only makes simple substitutions, for security purposes, A and B should be surrounded by parentheses, and the entire expression should be surrounded by parentheses.

This "function" usage is the same as a normal function and is faster. However, it is prone to difficult to detect errors. Therefore, use the inline function (inline) instead of the macro definition.

Note that you do not change the value of the variable in Parameters !

4. Fourth usage-simplify a piece of code :

#define MOVE (dx, dy) if (Isfull (dir)) return; if (Map (X+DX, y+dy) = = ' 0 ') {push (Dir,x+dx,y+dy,head[dir], DEP); check (dir);}

  

Don't forget "\" after each line, which is equivalent to a newline character. This move simplifies a large piece of code. Of course, inline functions in C + + can also be implemented in this function.

Definition and usage of macros in C + + (now replaced by inline functions)

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.