C + + macros

Source: Internet
Author: User

Some of the more useful macros that the system comes with

__TIME__ system Current time, expressed as h:m:s

__file__ file name

__date__ Date

__line__ to line number of file code lines

Macro----with parameters "macro function

The function of a macro is to replace all

#include <iostream>#defineSWAP (t, m, n) {t t=m;m=n;n=t;};intMain () {using namespacestd; intA =Ten, B = -; Doublec =12.3, d =45.6; SWAP (int, A, b) SWAP (int, C, D) cout<< a <<"  "<< b <<Endl; cout<< C <<"  "<< D <<Endl; System ("Pause"); return 0;}

The following is a pre-compiled code

#include <iostream>#defineSWAP (t, m, n) {t t=m;m=n;n=t;};intMain () {using namespacestd; intA =Ten, B = -; Doublec =12.3, d =45.6; {intt = b; b = A; A =t;}; {intt = D; D = C; c =t;}; cout<< a <<"  "<< b <<Endl; cout<< C <<"  "<< D <<Endl; System ("Pause"); return 0;}

Here it is obvious to see the meaning of the whole substitution, is a word does not leak the replacement, do not think this is like a function to pass an argument to the copy copy

Just move the macro-defined code to the corresponding line of code intact.

Several problems with parametric macros

1: Try to enclose the parameters as far as possible

#include <iostream>#defineSWAP (t, m, n) {t t=m;m=n;n=t;};#definePI 3.14159#defineArea (R) pi*r*r//pi* (R) * (R) can get the results we want, so the parameters defined in the macro are usually enclosed in parentheses  .intMain () {using namespacestd; intA =Ten, B = -; Doublec =12.3, d =45.6; cout<< area (a) <<Endl; //Output result 314.159 cout<< Area (a+b) <<Endl;  //passed in expression, output 215.416cout      << pi*a + b*a +  b;  //Output 215.416 replaced, and did not follow the expected results, the operator's priority so that our results have changed the system (  "Pause"); return 0;}

2: Expression as far as possible.

#include <iostream>#defineSWAP (t, m, n) {t t=m;m=n;n=t;};#definePI 3.14159#defineArea (R) pi*r*r#defineMAX (x, y) x>y?x:y//enclosed in parentheses (x>y?x:y) so we can get the results we want.intmain ()// I want to return two maximum values here, and the result is a return ofusing namespacestd; intA =Ten, B = -, E = -, F = -; Doublec =12.3, d =45.6; cout<< (MAX (A, B) +MAX (e,f)); //The result after preprocessing is that x>y?x:y+x>y?x:y; is not the result we need, but the solution is to define the system in the macro ("Pause"); return 0;}

There are many other problems with macros, such as the + + operator, but just remember that the actions of the macro are all original replacements, and the corresponding characters are replaced to find out where the error is.

The use of two other special characters in a macro

#, convert the argument to a string such as str (hello), I'm typing just hello, but I define the STR (r) #r in the macro, then R is converted to a string and can be output smoothly.

# #, stitching identifiers

#include <iostream>#defineSWAP (t, m, n) {t t=m;m=n;n=t;};#definePI 3.14159#defineArea (R) pi*r*r#defineMAX (x, y) x>y?x:y#defineWelcome (WHO) welcome# #who ()using namespacestd; voidWELCOMEAA () {cout <<"A"<<Endl;}; voidWelcomebb () {cout <<"B"<<Endl;};intMain () {welcome (AA); System ("Pause"); return 0;}

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.