Inline functions and macros

Source: Internet
Author: User

Macro, we sometimes define some preprocessing macros, such

# Define comp (x)> 0? (X): 0) defines a macro.

The reason for macro usage is that function calls have a certain amount of time and space overhead (when the function call user jumps to the address in the memory of the function, after the execution, it jumps back to the original position to continue the execution. This transfer requires saving the scene and remembering the current memory location, and restoring the scene after the function is executed), while the macro only expands the code in the pre-processing place without additional time space overhead, therefore, the efficiency is improved.

Inline functions are defined using inline. functions that define the function body within the class are called inline functions by default.

Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. Read/write operations must be performed using the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.

Class test {

 

PRIVATE:

 

Int test;

 

Public:

 

Int gettest () {return test ;}

 

Void settest (INT t) {test = T ;}

 

}

 

The difference between an inline function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues.

Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.

Extracted from http://blog.csdn.net/longjing1113/article/details/8807671

 

Inline functions and macros

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.