Inline and macro

Source: Internet
Author: User

The _ inline keyword has been compiled and tested using keil4 arm. The difference between macro definition and inline function is verified.
  1. Both macro-defined and inline functions do not generate code segments, and the program runs without the parameter pressure stack overhead.
  2. The Inline Function checks the validity of the called parameter, and can also convert the parameter type.
I. The Inline keyword is used to define the inline function of a class. The main reason for introducing it is to use it to replace the macro definition in expression form in C. An example of macro definition in expression form: # define expressionname (var1, var2) (var1) + (var2) * (var1)-(var2) Why should we replace this form, and let me know: 1. first, let's talk about the reason why macro definition is used in C. C language is a very efficient language. This macro definition is like a function in form and usage, but it is implemented using a Preprocessor, noParameter pressure stack, code generationAs a result, the efficiency is very high, which is one of the main reasons it is used in C. 2. this macro definition is similar to a function in form, but when using it, it only performs simple replacement in the pre-processor symbol table, so it cannot detect the parameter validity, therefore, the C ++ compiler cannot strictly check the type, and its return value cannot be forcibly converted to a suitable type for conversion, its use has a series of risks and limitations. 3. the Class and Class access control are introduced in C ++. In this way, if an operation or expression involves the protection or private member of the class, you cannot use this macro definition (because you cannot place this pointer in a proper position ). 4. The purpose of inline is to replace the macro definition of this expression form. It eliminates its shortcomings and inherits its advantages. Why does inline replace the definition of expression format? 1-3 points are described as follows: 1.Inline functions of classes defined by inline. The code of the functions is put into the symbol table and replaced directly during use (expanded like a macro), without the call overhead and high efficiency.2. Obviously, the inline function of a class is also a real function. When the compiler calls an inline function, it first checks its parameter type to ensure that the call is correct. Then perform a series of related checks, just like treating any real function. This eliminates its hidden dangers and limitations. 3. inline can be used as a member function of a class. Of course, you can use the protected member and private member of the class. When to use the inline function: First, you can use the inline function to completely replace the macro definition in the expression form. In addition, it should be noted that inline functions are generally used only when the function content is very simple. This is because the code of inline functions is expanded wherever it is called. If the function is too complex, the consequence of code expansion is likely to be greater than the benefits of efficiency improvement. The most important use of inline functions is for class access functions. How to Use the inline function of the class: A Brief Introduction to the use of inline: 1. define this function in the class: Class classname {......... getwidth () {return m_lpicwidth;}; // if it is directly defined in the class, you do not need to use inline to modify ........} 2. declare in the class and define it outside the class: Class classname {......... getwidth (); // if it is directly defined in the class, you do not need to use inline to modify ........} inline return_type classname: getwidth () {return m_lpicwidth ;}

Reprinted from: http://blog.csdn.net/yangtalent1206/article/details/6049718

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.