Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ inline member function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C++ inline member functions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use C++ inline member functions.

What is an inline function:

Inline function is an enhancement of C++, which can reduce the execution time of the program. Functions can become inline functions by instructing the compiler so that the compiler can replace those function definitions that are called. The compiler replaces the definition of the inline function at compile time instead of referencing the function definition at run time.

Note: this only advises the compiler to inline the function. If the function is large (in terms of executable instructions, etc.), the compiler can ignore the "inline" request and treat the function as a normal function.

How to make a function inline:

To make any function inline, use the keyword "inline" at the beginning of its definition.

Example:

The first case: class A {public: inline int add (int a, int b) {return (aqb);}}; the second case: class A {public: int add (int a, int b);}; inline int A::add (int a, int b) {return (aqb);} third case: inline int add_two (int a, int b) {return (aqb);}

You can define a member function in its class definition, or if you have declared (but not defined) the member function in the class definition, you can define it outside.

The first situation:

When the member functions defined in the class member list are inline member functions by default, the inline keyword can also be omitted from the first class A definition.

Member functions that generally contain several lines of code are usually declared inline, or shorter functions can be defined in the definition of the class.

The second situation:

If you define a member function outside the class definition, it must appear within the namespace that surrounds the class definition. You must also use the scope resolution (::) operator to qualify the name of the member function.

At this point, if you want to declare an inline function, you can declare it with the inline keyword in the class (and define the function outside of its class), or define it with the inline keyword outside the declaration of the class.

The second class An above uses the inline keyword at the definition.

The third situation:

A normal global function that can add the inline keyword at the declaration or definition.

In the following example, the member function Ypurpurf () is an inline member function:

Link Properties:

Inline modifiers do not affect the link properties of member or non-member functions: links default to external links.

An internal link indicates that it is accessible only within the current file, and an external link indicates that multiple files are accessible.

The member function of a local class must be defined in its class definition. Therefore, the member function of a local class is an implied inline function. These inline member functions do not have link properties.

Why use inline:

In many places, we create functions for small work / functions that contain a simple and small number of executable instructions. Imagine their overhead each time they are called by the caller.

When a normal function call instruction is encountered, the program will store the memory address of the instruction immediately following the function call statement, load the called function into memory, copy the parameter values, jump to the memory location of the called function, execute the function code, store the return value of the function, and then jump back to the instruction address that was saved just before the called function was executed. Running time is too expensive.

C++ 's inline function provides an alternative. Using the inline keyword, the compiler replaces the function call statement with the function code itself, and then compiles the entire code (this process becomes code expansion). Therefore, with inline functions, the compiler does not have to jump to another location to execute the function and then jump back, because the code of the called function is already provided to the caller.

Through the following advantages, disadvantages, and performance analysis, you will be able to understand why the "inline" keyword is used.

Advantages:

1. It avoids the overhead of function calls, thus speeding up program execution.

two。 When a function call occurs, it saves the overhead of push/pop variables on the stack.

3. It saves the overhead of returning the call from a function.

4. It makes more use of local references by using instruction caching.

5. By marking it inline, you can put the function definition in the header file (that is, it can be included in multiple compilation units without being complained by the linker).

Disadvantages:

1. As the code expands, the size of the final executable is increased.

2. C++ inlining is handled at compile time. This means that if you change the code of an inline function, you will need to recompile all code that uses it to make sure it is updated.

3. When used in a header file, it makes your header file larger because the user does not care about the information.

4. As mentioned above, it increases the size of the executable file, which can lead to memory jitter. More page failures will degrade the performance of your program.

5. Sometimes it is not practical, for example, in embedded systems, due to storage space constraints, it is necessary to ensure that the executable file is as small as possible.

Key points:

1. Inline functions are only a suggestion, not mandatory. The compiler may or may not inline the functions you mark as inline. Functions that are not marked inline may also be set to inline when compiled or connected.

two。 Inline works like a compiler-controlled copy / paste, which is completely different from a preprocessor macro. Macros are forcibly inlined, pollute all namespaces and code, and are not easy to debug.

3. All member functions declared and defined in the class are inline by default. So it doesn't need to be clearly defined as inline.

4. Virtual functions do not support inlining. However, sometimes, when the compiler can determine the type of the object (that is, the object is declared and constructed in the same function body), even a virtual function is inlined because the compiler knows exactly the type of the object.

5. Template methods / functions are not always inlined (their presence in the header file does not automatically inline them).

6. Most compilers inline recursive functions, and some have switches for this function and can set the maximum depth of recursion.

At this point, I believe that everyone on the "C++ inline member functions how to use" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report