In depth use of the inline keyword in C++

  • 2020-04-01 23:37:43
  • OfStack

I. in C&C++
The inline keyword is used to define the inline function of a class. The main reason for its introduction is that it replaces the macro definition in the form of an expression in C.
      Example of a macro definition in the form of an expression:
      Define ExpressionName(Var1,Var2) ((Var1)+(Var2))*((Var1)-(Var2))  
      1. First, let's talk about the reason for using this form macro definition in C. C is a very efficient language. This macro definition is like a function in form and use, but it USES prefixes
The manager implementation, which has no parameter pressing, code generation, etc., is very efficient, which is one of the main reasons why it is used in C.
      2. This macro definition is formally similar to a function, but when used, it does nothing more than a simple substitution in the preprocessor symbol table, so it does not check for parameter validity
, which does not enjoy the benefits of strict type checking by the C++ compiler, and its return value cannot be cast to a suitable type that can be converted
Pitfalls and limitations.
      3. C++ introduced access control for classes and classes, so that if an operation or an expression involves a protected or private member of a class, you cannot use this type of macro
To implement (because this pointer cannot be placed properly).
      4. Inline is also designed to replace this macro definition in the form of an expression, eliminating its disadvantages while inheriting its advantages.
 
Why is inline a good alternative to the predefined inline?
Corresponding to the 1-3 points above, it is expounded as follows:
      1. Inline function of the inline class, the code of the function is put into the symbol table, when used directly to replace, (like a macro expansion), no call overhead, efficiency
Is very high.
      2.   Obviously, the inline function of a class is also a real function, and when the compiler calls an inline function, it first checks the type of its argument to make sure the call is correct. then
A series of correlation checks, just like any real function. This eliminates its hidden dangers and limitations.      
      3. Inline can be used as a member function of a class, where, of course, the protected and private members of the class can be used. When to use the inline function: first, you can use the inline function
Replace the macro definition in the form of an expression completely with the inline function. Another thing to note is that inlined functions are generally only used when the contents of the function are very simple, because the code for the inlined function will be in
Expand it wherever it is called, and if the function is too complex, the bad effects of code bloat are likely to outweigh the benefits of increased efficiency. The most important use of inline functions is for classes
Access function.

Iii. How to use the inline function of the class:
Simply mention the use of inline:
      1. Define such a function in a class:

    class ClassName
    {   
        .....   
        ....   
        inlline GetWidth(){return m_lPicWidth;} //If defined directly in the class, use inline
        ....   
        ....   
   };   

    2. Declare in the class and define outside the class:

   class ClassName
   {   
    .....   
    ....   
    GetWidth(); //If you define it directly outside the class, you can do without the inline modification
    ....   
    ....   
   };  


Related articles: