The inline function inline is further resolved with the macro definition

  • 2020-04-02 01:41:02
  • OfStack

Advantages of inline functions:
I: inline function of the inline class defined by inline, the code of the function is put into the symbol table, when the use of direct replacement (like the macro definition), there is no overhead of the call, very efficient.
The convergent function of a class is a real function.
Use the inline function inline to completely replace the macro definition in the form of an expression.

Example:


Class A
{
public : 
int readTest (a) 
{
return nTest ; 
}
void setTest ( int i);
};
inline void A::setTest(int i)
{
nTest=i;
};

Description: The member functions of class A, readTest () and setTest (), are inline. The body of the readTest () function is placed in the class declaration, so readTest () is automatically converted to the inline function. The body of the setTest function is outside the class declaration, so the inline keyword is added.

Summarize the differences between the inline function and the macro definition:
1. The conversions are expanded at compile time, while macros are expanded at precompile time
2. An introverted function can be embedded directly into the target function code at compile time, whereas a macro is simply a text substitution
3, the convergent function can be completed such as the type of detection, statement is correct and other compilation functions, macros do not have this function
Macros are not functions, while the inline function is a function
5. Macro parameters should be carefully handled when defining macros (generally, they are enclosed in brackets).


Related articles: