First, in the c&c++
The inline keyword is used to define an inline function for a class, and the main reason for introducing it is to use it instead of a macro definition in the form of an expression in C.
An example of a macro definition in the form of an expression:
#define EXPRESSIONNAME (VAR1,VAR2) (VAR1) + (VAR2)) * ((VAR1)-(VAR2)) Why replace this form, and listen to my word:
1. First, let's talk about the reason for using this form of macro definition in C, which is a highly efficient language, which is defined in terms of form and use like a function, but it uses the pre
The realization of the manager, without the parameters of the stack, code generation, such as a series of operations, therefore, high efficiency, which is the main reason it is used in C.
2. This macro definition is similar in form to a function, but when used, it is only a simple substitution in the preprocessor notation table, so it cannot be tested for parameter validity.
, you cannot enjoy the benefits of strict type checking by the C + + compiler, and its return value cannot be coerced into the appropriate type that can be converted, so that it uses a series of
Pitfalls and limitations.
3. Class and Class access control is introduced in C + +, so that if an action or an expression involves a protected member of the class or a private member, you cannot use this macro-setting
To implement (because the this pointer cannot be placed in the right place).
4. The purpose of inline is to replace the macro definition in this form of expression, which eliminates its drawbacks while inheriting its advantages.
Second, why inline can be a good substitute for the predefined?
Corresponding to the above 1-3 points, elaborated as follows:
1. Inline the inline function of the class defined, the code of the function is put into the symbol table, replaced directly when used (like a macro), without the overhead of the call, and efficiency
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 arguments to ensure that the call is correct. and then proceed
A series of related checks, just like treating any real function. This eliminates its hidden dangers and limitations.
3. Inline can be a member function of a class, and of course you can use the protection and private members of the class in it. When to use the inline function: first, you can make
Use the inline function to completely replace the macro definition in the form of an expression. Also note that inline functions are typically used only when the content of the function is very simple, because the code for the inline function is
Wherever it is invoked, if the function is too complex, the consequences of code bloat are likely to outweigh the benefits of increased efficiency. The most important place to use inline functions is for classes
Access functions.
Iii. How to use the inline function of a class:
Simply mention the use of inline:
1. Define this function in the class:
Copy Code code as follows:
Class ClassName
{
.....
....
Inlline getwidth () {return m_lpicwidth;}//If defined directly in class, require inline modification
....
....
};
2. Declare in class, define outside the class:
Copy Code code as follows:
Class ClassName
{
.....
....
GetWidth (); If you define directly outside the class, you can not use the inline modifier
....
....
};