What is the difference between inline functions and outliers?

Inline function

Inline functions are functions that are modified with the inline keyword. Functions defined within a class are defaulted to inline functions. Inline functions look at the source code layer and have the structure of the function, but after compiling, they do not have the nature of the function.

Inline functions do not control transfer when called, but embed function bodies at each call at compile time. At compile time, similar to macro substitution, use the function body to replace the function name at the call. Generally used inline modification in the code, but whether you can form an inline function, you need to see the specific processing of the function definition of the compiler.

Inline extensions are used to eliminate the time overhead of function calls. It is usually used for functions that are executed frequently, and is very beneficial for functions with small memory space.

If there are no inline functions, the compiler can decide which functions are inline. Programmers have little or no control over which ones can only be inlined and which ones can't be inlined. The role is that programmers can choose inline specific applications.

What is the difference between inline functions and outliers?

The difference between inline function and out-of-line function

A member function declared or defined inside a class declaration is called an INLINE function.

There are two ways to do this:

1. Internal declarations in class declarations, and external definitions in class declarations are called explicit inline functions, such as:

Class display

{

Int t;

Public:

Void output(void)

}

Display object;

Inline void display::output(void)

{

Cout ""i is"" "i" "/n";

}

2. Internal definitions in class declarations, called implicit inline functions, such as:

Class display

{

Int t;

Public:

Inline void output(void)

{cout" "i is "" "i" "/n";}

}

Application and precautions

The purpose of introducing inline functions is to solve the efficiency problem of function calls in the program.

A function is a more advanced abstraction. Its introduction makes the programmer only care about the function and usage of the function, and does not care about the specific implementation of the function; the introduction of the function can reduce the object code of the program and realize the sharing of the program code and data. However, the function call also brings the problem of reducing efficiency, because the calling function actually transfers the program execution order to an address stored in the function, and after the program content of the function is executed, it returns to execute. The place before the function. This transfer operation requires that the site be protected and the address of the execution be memorized before the transfer. After the transfer, the site must be restored and the original save address is continued. Therefore, function calls have a certain amount of time and space overhead, which will affect their efficiency. Especially for some functions whose function code is not very large, but is frequently called, it is more important to solve the efficiency problem. Introducing inline functions is actually to solve this problem.

At compile time, the compiler replaces the call expression of the inline function that appears in the program with the body of the inline function. Obviously, this kind of practice will not cause the problem of turning back, but since the code in the body of the function is replaced in the program at compile time, it will increase the amount of the target program code, thereby increasing the space overhead, but not the time overhead. As large as the function call, it can be seen that it is in exchange for time savings at the expense of the increase in the target code.

In a program, when a function is called, the function is replaced at compile time, rather than being called at runtime like a normal function.

Precautions:

Things to be aware of when using inline functions

Inline functions have the characteristics of general functions, which differ from general functions only in the processing of function calls. When a general function is called, the program execution right is transferred to the called function and then returned to the function that called it. When the inline function is called, the calling expression is replaced with the inline function body. When using inline functions, you should pay attention to the following points:

1. Loop statements and switch statements are not allowed in inline functions.

If the inline function has these statements, the compiler will generate the function call code as if it were a normal function. The recursive function (which calls its own function) cannot be used as an inline function. Inline functions are only suitable for small functions with only 1 to 5 lines. For a large function with many statements, the overhead of function calls and returns is relatively insignificant, so there is no need to implement them with inline functions.

2. The definition of the inline function must appear before the inline function is called for the first time.

3. All the functions defined inside the class description in the class structure mentioned in this column are inline functions.

Smart Phone Holder For Car Vent

Smart Phone Holder For Car Vent,Air Vent Iphone Mount,Air Vent Phone Holder,Air Vent Cell Phone Pouch Holder

Ningbo Luke Automotive Supplies Ltd. , https://www.nbluke.com