Fundamentals of C + + programming (4) macro definition and inline

Source: Internet
Author: User
Tags mul

1. Knowledge Point 1.1 macro definition

(1) macro definition without parameters

1 #define error_message-1002#define seconds_per_day 60*60*60

(2) with the parameter macro definition, this form is called the macro function, but actually is not the function

#define Outputint (x) cout<< "INT:" <<x<<endl#define outputchar cout<< "CHAR:" <<x< <endl
1.2 Inline functions

Macro definition is in the preprocessing phase of macro expansion, but often there are some unexpected errors, so there are inline functions, the inline function has not only played the advantages of macro definition, but also compensate for its shortcomings.

Inline functions are defined at the top of the function with inline, or are defined at the same time as the function declaration (this method is not recommended).

The following is an example of an inline function:

1 classRectangle {2  Public:3Rectangle (int,int);4     intGetsquare ();5     intGetgirth () {return 2* (length, width); }//define functions directly at declaration time, forming inline functions6 Private:7     intlength;8     intwidth;9 };Ten  OneRectangle::rectangle (intLintW): Length (L), Width (w) {} AInlineintRectangle::getsquare () {//use inline to form inline functions when defining functions -     returnLength *width; -}
2. Question 2.1 Briefly describe the differences between inline functions and macro definitions

The same point: both can save the time and space consumed by the frequent function calls, and improve the efficiency of the execution. Both of them. Replace the function call with the complete function body, and the implementation is similar.

Difference: (1) The fundamental difference between the two is that the macro definition is simply a substitution of strings, not a function, and an inline function is a function.

(2) The code expansion of the two occurs at different stages, the macro definition is expanded during the preprocessing phase, and the inline function is expanded during the compilation phase.

(3) When an inline function acts as a member function of a class, you can access all members of the class, including public, private, protected members, implicitly using the this pointer, and the macro definition cannot implement these functions.

(4) The inline function can completely replace the macro definition, so use the macro definition as little as possible.

(5) In addition, in the use of inline functions to pay attention to the code expansion problem, the inline function should be as short as possible (in addition, now the compiler generally has an optimization function, when the inline function code is detected long, not inline, even if the use of inline functions).

2.2 Macro-defined macro expansion errors

Indicate the error in the macro definition in the following program and modify

1 #defineMAX (A, b) a>b?a:b2 #defineMUL (A, b) a*b3 intMainintargcChar*argv[]) {4     intx =4, y =2;5     intMax =MAX (x, y);6     intProduct =MUL (x, y);7cout <<"The Max is"<< Max <<Endl;8cout <<"The product is"<< Product <<Endl;9 GetChar ();Ten     return 0; One}

Knowledge point: Macro definition of its own defects is mainly after the macro expansion, due to the precedence of operators and other reasons, so that the macro definition expanded semantics and expected deviation.

The following are examples of two macro expansion errors

1 intProduct=mul (x,y+3)2 intMax=max (x, y) +23 4 //the intention is5 intproduct=x* (y+3)6 intmax= (x>y?x:y) +27 //The actual macro expansion becomes8 intproduct=x*y+39 intmax=x>y?x:y+2

The solution includes the following two points:

(1) Add parentheses to the parameter itself

(2) Add parentheses to the entire macro definition

The result of the modification is as follows:

#define MAX (a) (a) > (b)? ( A):(B))#define MUL (a) (a) ((a) * (b))
2.3 Common sense issues with inline functions

The following about the inline function description error is

(A) The inline function can be overloaded;

(B) A constructor can be defined as an inline function;

(C) inline functions can reduce the overhead of function calls;

(D) Inline functions should use the inline keyword when declaring a function

Answer: D, be sure to use inline at the time of definition, and use it at the time of declaration does not play any role.

Fundamentals of C + + programming (4) macro definition 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.