C ++ analysis series talks (3)

zhaozj2021-02-11  185

C Differential Series Talk (3) Zheng Liqun

After talking about constings, this article is again on this keyword, which puts this article in this position because the introduction of the keyword inline is very similar, and below is divided into the following sections. Equalize.

Causes of Inline keywords in C :

The Inline keyword is used to define a class of inline functions, introducing its main reason is to use it to replace the macro definition in the expression in C.

An example of a macro definition in expression:

#define expressionname (var1, var2) * (var1-var2)

Why replace this form, and listen to me:

1. First, talk about the reason for using this form macro in C, C language is a high efficiency language, which is defined in the form and uses a function like a function, but it uses the preprocessor implementation, no parameter pressure A series of operations such as stack, code generation, therefore, the efficiency is high, this is a major reason it is used in C.

2. This macro definition is similar to a function in the form, but when using it, it is just a simple replacement in the pre-processor symbol table, so it cannot be detected by parameter validity, and cannot enjoy the C compiler strict type. The benefits of checking, and its return value cannot be enforced to convert to a suitable type of convertible, so that its use has a series of hidden dangers and limitations.

3. Introducing access control of classes and classes in C , so that if an operation or an expression involves a category protection member or private member, you cannot use this macro definition (because you can't put this pointer. In a suitable location).

4. The purpose of Inline is also to replace this expression of the macro, which eliminates its shortcomings, and inherits its advantages well.

Why is INLINE to replace the form of formula formula?

Corresponding to 1-3 points above, as follows:

1. Inline defined class's inline function, the code of the function is placed in the symbol table, directly replacing it directly, (like macro is unavoidable), there is no call overhead, the efficiency is also very high.

2. Obviously, the inline function of the class is also a real function. When calling an inline function, the compiler will first check the type of the parameters, and ensure that the call is correct. Then make a series of related inspections, just like any of the true functions. This eliminates its hidden danger and limitations.

3. Inline can be used as a member of a class, of course, can use the protection member and private member of the class. When I use the inline function:

First, you can use the inline function to completely replace the macro definition of the form of the form.

Also note that the inline function will only be used very simple in the function content, because the code of the inline function will be expanded in any place to call it. If the function is too complex, the evil consequences of code expansion is likely to The benefits that will be greater than the improvement of efficiency. The most important use place for inline functions is the access function for class.

How to use classes of the inline function:

Simply mention the use of inline:

1. Define this function in the class:

Class classname {

.....

....

GetWidth () {return {RETURN M_LPICWIDTH;}; // If you are directly defined in the class, you can do not use inline modification

....

....

}

2. Declare in the class, definition in the class:

Class classname {.....

....

GetWidth (); // If you are directly defined in the class, you can do not use inline to modify

....

....

}

Inline getWidth () {

Return m_lpicwidth;

}

In this article, talk about a special function, class's Inline function, its source and characteristics are very similar to const in some point, can look up with Const.

转载请注明原文地址:https://www.9cbs.com/read-4544.html

New Post(0)