In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the "C++ inline function, callback function and ordinary function instance analysis" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
1. Inline inline function #
Features
It is equivalent to writing the contents of the inline function at the place where the inline function is called.
It is equivalent to executing the function body directly without performing the step of entering the function.
It is equivalent to a macro, but it has more type checking than a macro, and it really has functional properties.
Compilers generally do not inline functions that contain complex operations such as loops, recursion, switch, etc.
Functions defined in class declarations, except for virtual functions, are automatically implicitly treated as inline functions.
Inline keyword is to recommend the compiler inline at compile time, whether the inline function depends on the compiler, a good compiler will automatically cancel the unworthy inline according to the definition body of the function (whether inline: 1, you can call the function many times to check the size of the execution file, if it becomes larger, it is proved to be an inline function; 2, view the data through disassembly).
1.1 use #
Inline is a "keyword for implementation", not a "keyword for declaration", that is, it is useless to use inline only in life. To become an inline function, you must add this keyword when defining the function. It doesn't matter whether or not to add the inline keyword to the declaration, but for ease of reading, it is recommended that both the declaration and definition be added.
When C++ defines a function in a class, the compiler implicitly inlines when the function does not contain complex operations such as loops, recursion, switch, and so on.
C++ defines a function outside the class because, unlike non-inline functions, the inline function must be visible to the compiler so that it can be expanded at the call point, and the inline function must be defined in every text file that calls the function. So the declaration and definition of inline functions are recommended in the same header file, so that another .cpp file # include the header file, the definition of the inline function is also included, which can be used normally.
Statement
/ / statement 1 (plus inline, recommended) inline int functionName (int first, int second,...)
Define
/ / define inline int functionName (int first, int second,...) {/ * /}
In-class definition
/ / in-class definition, implicitly inline class A {int doA () {return 0;} / / implicitly inline}
Out of class definition
/ / out-of-class definition, need to explicitly inline class A {int doA ();} inline int A::doA () {return 0;} / / need explicit inline 1.2 compiler to handle inline function step #
Copy the inline function body to the point where the inline function is called
Allocate memory space for local variables in the inline function you use
Map the input parameters and return values of the inline function to the local variable space of the calling method
If the inline function has more than one return point, turn it into a branch at the end of the inline function code block (using GOTO).
1.3 advantages and disadvantages #
1.3.1 advantages #
The inline function, like the macro function, will expand the code at the place to be called, eliminating the parameter stack, stack frame opening and recovery, and the result return, so as to improve the running speed of the program.
Compared with macro functions, inline functions do security checks or automatic type conversions when code is expanded (like normal functions), while macro definitions do not.
Declare member functions that are also defined in the class and automatically convert them to inline functions, so inline functions can access the member variables of the class, while macro definitions cannot.
Inline functions are debuggable at run time, while macro definitions are not.
1.3.2 be careful with inline #
Inlining is at the cost of code expansion, which only saves the overhead of function calls, thus improving the efficiency of function execution. If the time it takes to execute the code in the body of the function is more expensive than the function call, then the efficiency gain will be very small. On the other hand, every inline function call will copy the code, which will increase the total code of the program and consume more memory space.
The constructor and destructor of a class can easily be misunderstood as being more efficient in using inline functions. Be careful that constructors and destructors may hide behaviors, such as "secretly" executing the constructors and destructors of a base class or member object. So don't casually put the definition bodies of constructors and destructors in the definition of the class.
1.3.3 inline is not recommended #
If the code in the function is long, using inline will result in high memory consumption.
If there is a loop inside the function, it takes more time to execute the code inside the function than it costs to call the function.
1.4 can virtual functions (virtual) be inline functions (inline)? #
Virtual functions can be inline functions, inline can modify virtual functions, but when virtual functions show polymorphism, they cannot be inline.
Inlining is when the compiler advises the compiler to inline, and the polymorphism of the virtual function is at run time, and the compiler cannot know which code to call at run time, so the virtual function cannot be inlined when it is polymorphic (runtime).
The only time inline virtual can be inlined is when the compiler knows which class the object is calling (such as Base::who ()), which occurs only if the compiler has a pointer or reference to the actual object rather than the object.
The following routines:
# include using namespace std;class Base {public: inline virtual void who () {cout
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.