Macros and inline functions

Source: Internet
Author: User

Macro: Use a string to replace an expression code or function call code. Before compilation, the Preprocessor uses the expression code or function call Code represented by this macro string to replace all the macro strings that appear. In this case, the function call Code represented by a macro does not need to open up the function stack, and does not need to protect and restore the function call site. This improves the code execution efficiency, calling a macro is more effective than calling a function;
However, when macro is called, there is a drawback: the macro is replaced by a macro string during preprocessing, so the compiler does not know the syntax of the Code represented by the Macro during compilation, I do not know the Data Type of the Code represented by macros, and there is no security check. In a class that appears, macros cannot be a private member of the callback class and are prone to ambiguity. Therefore, macros also have unavoidable problems that they cannot solve;
The solution to the problem caused by macro-defined functions is to use inline functions. inline functions can be used instead of macro-defined functions;

Inline functionsThe difference with macros Is That Macros are replaced by pre-processors with no syntax check, type check, and security check. inline functions are implemented through compiler control, syntax check, type check, and security check are available. inline functions are real functions and are called, the compiler is responsible for replacing the function body code block of the inline function with the place where the inline function is called. This is similar to macro replacement. inline functions have parameters and return values; since inline functions can be expanded like macros, when calling inline functions, the overhead of function parameter pressure stacks and output Stacks is removed, thus reducing the overhead of function calls; this is where inline functions are superior to macros;

The description of the inline function and the definition of the function body of the inline function must be the same. The statements that declare the inline function below are invalid:

Inline int Max (int a, int B );

The following inline function definitions are valid:

Inline int Max (int a, int B) {return (a> B )? A: B )};

Member functions of the C ++ class can also be defined as inline functions. For example:

Class Student
{
Private:
Int nID
Int nAge;
Float fScore;
Public:
Void setID (int nid) {nID = nid;} // This member function is automatically inline by default (implicit inline function definition)
Int getID (void) {return nID;} // This member function is an inline function by default (implicit inline function definition)
Inline void setAge (int nage) {nAge = nage;} // explicitly defines the inline function
Inline int getAge (void) {return nAge;} // explicitly defines the inline function
Void setScore (float fscore); // The class definition body is not declared as an inline function;
Float getScore (void); // The class definition body is not declared as an inline function;
}
Inline void Student: setScore (float fscore) {fScore = fscore;} // class defines in vitro implementation as an inline function;
Inline float Student: getScore (void) {return fScore;} // class defines in vitro implementation as an inline function;

 

In C ++, a member function of the function body is defined inside the class definition body, and is considered as an inline function by the compiler by default, regardless of whether the keyword inline exists before the function header. For example: setID (), getID (), setAge (), and getAge (); you can also define member functions that are implemented outside the class definition body as inline functions, at this time, only the member function header is declared in the class definition body, and its implementation is outside the class definition body, such as setScore () and getScore ();

Even if you can define a member function that is implemented outside the class definition body as an inline function, the implementation of this member function must also be written in the header file of the class definition body (. h). The rule cannot be violated. That is, the definitions and implementations of inline functions must be in the same header (. h) file;

Limitations of inline functions:

1. Because inline functions are implemented with code replacement like macros, the function body defined as inline functions should not be too large. If the function body is too large, some common compilers will discard the inline method, instead, the method of calling common functions loses the meaning of inline functions. Therefore, the function body code of inline functions should not be too large, which is usually 3-4 lines of code;

2. Because inline functions are expanded by the compiler during compilation, the definitions and implementations of inline functions of classes must be included in the header file of the declared class, instead, it cannot be placed in the cpp file of the implementation class. This is similar to the template <>;

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.