About inline functions
Keywords:inline
C + + inline functions are typically used with classes. If a function is inline, at compile time, the compiler places a copy of the function's code in each call to that function.
Any modification to the inline function requires that all clients of the function be recompiled, because the compiler needs to replace all of the code once, otherwise the old function will continue to be used.
If you want to define a function as an inline function, you need to place the keyword inlinebefore the function name, and you need to define the function before calling the function. If there are more than one row of defined functions, the compiler ignores the inline qualifier.
functions defined in a class definition are inline functions even if the inline specifier is not used.
Here is an example that uses an inline function to return the maximum value in two numbers:
#include <iostream>using namespaceStd;inlineintMax (intXinty) { return(x > Y)? x:y;//Returns x if x>y, otherwise returns y}//main function of the programintMain () {cout<<"Max (20,10):"<< Max ( -,Ten) <<Endl; cout<<"Max (0,200):"<< Max (0, $) <<Endl; cout<<"Max (100,1010):"<< Max ( -,1010) <<Endl; return 0;}
When the above code is compiled and executed, it produces the following results:
Max (0,tenmax (100 ,10101010
The difference between an inline function and a normal function
Inline functions must be simple code so that the internal statements of the function can be compiled directly, and if the function is too cumbersome, even adding inline does not compile in the way of inline functions. In addition, the loop body function cannot be compiled into an inline function.
In-class definitions and inline functions
As the function name and function body are written in the class inside the function form is called the definition of the class, the compiler will compile it by default to an inline function.
Class student{public: void Setage (int. _age) {age =int getage () {returnvoid Study () {}privatestringint age ;} ;
C + + inline functions