Macro # basic define usage

Source: Internet
Author: User

1. Define a simple constant: Define a constant to facilitate modification. (do not add a semicolon to the end !)

# Define n 1000

It is equivalent to const int n = 1000, but slightly different. Define is a simple replacement instead of a quantity.

 

2. Define simple functions: Pay attention to the use of parentheses

# Define max (x, y) (x)> (y ))? (X): (y)

 

3. Define a single row macro: There are three main usage types.

1) The prefix ##or Postfix ## is used as a part of a valid identifier. Note that it is not a string. It is often used in the macro definition of multiple rows. For example:

# Define a (x) T _ # x

Int A (1) = 10; // equivalent to int t_1 = 10;

# Define a (x) TX ##__

Int A (1) = 10; // equivalent to int T1 _ = 10;

 

2) Add # @ to convert the tag to the corresponding character. Note: only valid for single tag conversion (incorrect understanding ?)

# Define B (X) # @ x

B (a) is 'A', B (1) is '1'. But B (ABC) is not very effective.

 

3) add # to convert the tag to a string.

# Define C (x) # x

Then C (1 + 1) is "1 + 1 ".

 

4. Define multi-row macros: note the use of the slash. the last row cannot use a slash.

# Define declare_rtti (thisclass, superclass )\

Virtual const char * getclassname () const \

{Return # thisclass ;}\

Static int istypeof (const char * type )\

{\

If (! Strcmp (# thisclass, type )\

Return 1 ;\

Return superclass: istypeof (type );\

Return 0 ;\

}\

Virtual int ISA (const char * type )\

{\

Return thisclass: istypeof (type );\

}\

Static thisclass * safedowncast (ditkobject * o )\

{\

If (O & O-> ISA (# thisclass ))\

Return static_cast <thisclass *> (o );\

 

Return NULL ;\

}

 

5. Used for Conditional compilation: (common form)

# Ifndef _ aaa_h

# DEFINE _ aaa_h

// C/C ++ code

# Endif

 

6. Some precautions:

1) The definition cannot be repeated. Unless the definition is exactly the same. # define a (x )... And # define a are repeated definitions.

2) You can only define symbols without defining values. For example, # define aaa

Traps

1) parameters cannot be changed when macros are used.

Macro parameters are always sensitive and prone to errors. Many people think that macro definitions with parameters are somewhat similar to functions. But pay attention to their differences, as shown in the following code snippet:

1 # define square (A) (a) * ())

2 int square (int)

3 {

4 return a *;

5}

6

7 int nvalue1 = 10, nvalue2 = 10;

8 int nsquare1 = square (nvalue1 ++); // nsquare1 = 110, nvalue1 = 12

9 int nsquare2 = square (nvalue2 ++); // nsquare2 = 100, nvalue2 = 11

Similar definitions produce different results. The reason is macro character replacement. As in the preceding example, both a are replaced by the nvalue1 ++ parameter, so the nvalue1 auto-increment operation is executed twice. It is the side effect of the macro replacing its parameter values multiple times during expansion. To avoid such side effects, the simplest and most effective method is to ensure that the macro parameters do not change, as shown below.

10 # define square (A) (a) * ())

11

12 INT nvalue1 = 10;

13 int nsquare1 = square (nvalue1); // nsquare1 = 100

14 nvalue1 ++; // nvalue1 = 11

2) enclose multiple expressions defined by macros with braces.

If a macro definition contains multiple expressions, enclose them in braces. Without this braces, multiple expressions in the macro definition may only be executed in the first sentence, as in the following code snippet:

1 # define clear_cube_value (L, W, h )\

2 L = 0 ;\

3 W = 0 ;\

4 h = 0;

5

6 int I = 0;

7 For (I = 0; I <cube_acount; I ++)

8 clear_cube_value (cubes [I]. l, cubes [I]. W, cubes [I]. H );

A simple replacement of characters does not guarantee that multiple expressions are placed in the for loop because they are not enclosed in braces of the loop body. The correct method should be to enclose multiple expressions in braces to ensure that all expressions are executed, as shown in the following code snippet:

9 # define clear_cube_value (L, W, h )\

10 {\

11 L = 0 ;\

12 W = 0 ;\

13 h = 0 ;\

14}

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.