Inline function-the function declared with Inline is an inline function.
1. Why use inline functions?
-- In a program, a function is frequently called. During the running process, the program needs to jump to the function to execute the call. When the jump Reaches hundreds of times, will seriously affect the execution efficiency of the program.
2. Use of inline functions
In C ++, to solve this problem, use the keyword inline to declare a function, such:
Inline int func ();
The compiler will not create a real function, but copy all the code of this inline function to the calling function. In this way, the program does not need to jump back and forth during execution, thus improving the program running efficiency.
Note: inline functions can be used only when they need to be frequently called and the code is relatively simple.
3. Separation and merging of member function definitions and declarations
In general, we separate the definition and declaration:
Class A {public: inline void func (INT, INT); // The member function func () is the inline function void print (); // The member function print () is the inline function PRIVATE: int I; Int J ;}; void a: func (int x, int y) {I = x; j = y ;} void :: print () {STD: cout <"The point is" <"(" <I <"," <j <") \ n ";}
In this way, both the member functions func () and print () are inline functions.
When we merge the definition and declaration of member functions:
Class A {public: void func (int x, int y) {I = x; j = y} // The member function func () is the inline function void print () {STD:: cout <"The point is" <"(" <I <"," <j <") \ n ";} // The member function print () is an inline function PRIVATE: int I; Int J ;};
When the definition and declaration are merged, the member functions func () and print () are also inline functions by default!
-- Learn C ++ from scratch