Parsing an inline function

Source: Internet
Author: User

What is an inline function? Why use inline functions? calling a function requires a certain amount of time and space overhead.

A procedure that represents a function call, a 1 program executes a statement before a function call, 2 control of a process is transferred to the entrance of the called function, a parameter is passed at the same time, 3 executes the function body statement in the called function, and the 4 process returns to the next instruction of the calling function, bringing the return value of the function back to ; 5 then executes the part that is not executed in the keynote function. before 2 execution, note the address of the instruction at that time to "protect the scene" (note the information at that time), in order to continue execution after the function call, the process returned to the address noted previously, and according to the information under the record "Restore the scene", and then continue to execute, these are a certain amount of time. C + + improves an efficient way to embed the code of the called function directly into the keynote function when compiling the program, rather than turning the process out. This function, embedded in the keynote function, is called a "built-in function", also called an inline function.
Two, the use of the inline functionthe method for specifying inline functions is simple, just add a keyword inline to the left end of the first line of the function.Example:inline int max (int a, int b){return a > B? a:b;}
int main (){int ret = max (4,5);System ("pause");return 0;}
in the above program, the Max function is an inline function. Note: You can add inline at the same time as the function is declared and defined, or you can declare inline in just one place.
third, when to use inline?of course, the use of inline is also a cost, because inline is the insertion of code, so it will increase the length of the program, it is generally small and frequently used functions declared as inline functions. In the case of a small function, the cost of a function call can exceed the time it takes to execute the function itself, and defining it as inline can greatly save program run time. Complex statements, such as loops and switch statements, cannot be included in an inline function. Note that the inline declaration of a function is just a suggestion for the user to compile the system, and the compiler will decide whether to do so depending on the situation.
Iv. Inline member functionsa member function of a class can also be specified as an inline function. If a member function of a class is defined within a class, the default is an inline function, and if the member function of the class is defined outside the class, the declaration that is required by the add-in keyword will be displayed as an inline function. It is important to note that if you define an inline function outside of the class, you must place the definition of the class definition and member function in the same header file, or you will not be able to copy the function code to the function's call point at compile time. This member function is specified as an inline function only if the member function defined outside the class is small and the invocation frequency is high.

Parsing an inline function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.