Now let's talk about the keyword inline. inline is also one of the frequently used modifiers in C ++ programming languages. The reason why this problem is put here, this is because the inline keyword is introduced for a very similar reason as const, which is described in the following sections.
The reason why the inline keyword is introduced in c ++:
The inline keyword is used to define an 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:
Why should we replace this form? Let me say:
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, without a series of operations such as parameter pressure stack and code generation, the efficiency is very high, which is one of the main reasons why 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 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 class inline function:
Here is a brief introduction to the use of inline:
1. Define this function in the class:
2. declare in the class and define it outside the class:
In this article, we talk about a special function, the inline function of the class, which is similar to the const in terms of its source and characteristics. It can be combined with the const. In addition, many of my friends recently interacted with me via mail, talked about many issues and gave me a lot of inspiration. I would like to express my gratitude here.