On most machines, the function calls does a lot of work: Save the Register before calling the function, use the Register to restore the field after the call is completed, copy the function parameters, and the program jumps to the new location to execute ...
The introverted function (inline functions) does not have this problem, we can understand it simply: inline is "in line"
Why say "in line", first look at the declaration of the reserved function:
// inline version:find the shorter of Strings inline const string & Shorterstring ( const string &s1, string &s2) { S1:s2; // So when we do: cout << Shorterstring (S1, S2) << Endl; // In fact, it turns into: cout << ( S1.size () < S2.size ()? S1:S2) << Endl;
The introverted function feels like a macro substitution, but inline has a type check before it is replaced, avoiding macro errors
When declaring an inline function, the compiler has a judgment on the function: Is it reasonable to be an introverted function? If it doesn't make sense, even if we declare the inline compiler will also treat the function as a normal function.
member functions defined in the class body are generally small in size, and the process of calling a function is relatively expensive. The time spent calling a function is much larger than the execution time of all statements in the small-scale function body. To reduce the time overhead, if control structures such as loops are not included in the member functions defined in the class body, the C + + system automatically handles them as built-in (inline) functions.
That is, when a program calls these member functions, it does not actually execute the function's calling procedure (such as preserving the return address, etc.), but instead embeds the function code in the program's call point. This can greatly reduce the time overhead of calling member functions. C + + requires that the generic built-in function be declared with the keyword inline, but for member functions defined within the class, you can omit inline because these member functions have been implicitly specified as built-in functions. Such as:
classStudent { Public : voiddisplay () {cout<<"Num:"<<num<<endl;cout<<"Name:"<<name<<endl;cout<<"Sex:"<<sex<<Endl; } Private : intnum; stringname; Charsex; };
Where line 3rd
void display ()
can also be written
inline void display ()
Explicitly declare the display function as a built-in function.
The above two formulations are equivalent. For functions defined within a class, it is generally written inline.
It should be noted that if the member function is not defined in the class and is defined outside the class body, the system does not default to the built-in (inline) function, and the procedure to invoke these member functions is the same as the procedure for invoking the general function. If you want to designate these member functions as built-in functions, you should explicitly declare them with inline. such as:
classStudent { Public: inlinevoidDisplay ();//declare this member function to be a built-in function Private : intnum; stringname; Charsex; }; InlinevoidStudent::d isplay ()//defining the display function outside the class as a built-in function{cout<<"Num:"<<num<<endl;cout<<"Name:"<<name<<endl;cout<<"Sex:"<<sex<<Endl; }
It is important to note that if you define an inline function outside the class body, you must place the definition of the class definition and the member function in the same header file (or in the same source file), or you will not be able to displace it at compile time (the copy of the function code is embedded in the function call point). However, it is not conducive to the separation of the class interface and the implementation of the class, which is not conducive to information concealment. Although the execution of the program is more efficient, it is not a good idea from the point of view of software engineering quality. This member function is specified as a built-in function only if the member function defined outside the class is small and the frequency of the call is high.
inline functions in C + +