The difference between an inline function and a normal function:
The normal function has only one copy in memory, and anywhere in 3 steps when calling her:
①: Find this function
②: Perform this inline function
③: Returns the result (can be void), ends this function, and proceeds to execute the code below this function
But the call steps of the inline function are the same as the different functions, because he is not the same at compile time:
How to compile:
If this function is an inline function, at compile time, whenever this function is called, the compiler will copy a copy of this function into this place. Therefore, the execution of the inline function, there is no different function so many steps, directly executed.
The advantage is that the efficiency is relatively high, the number of places to call more, then copy more times, so that occupy more memory.
To have a simple inline:
#include <iostream>using namespace std;inline void Fun1 () {cout << "This is an inline function" << Endl;} int main () {fun1 (); return 0;}
Analytical:
Declare that the inline function needs to use the keyword: inline
Another area to be aware of:
①: function declaration (declared) and its implementation require the use of the inline keyword
#include <iostream>using namespace std;inline void fun1 (); inline void fun1 () {cout << "This is an inline function" << end l;} int main () {fun1 (); return 0;}
②: In-class definition-out-of-class implementations require the use of the inline keyword
#include <iostream>using namespace Std;class inlinec{public:inline void Fun ();}; inline void Inlinec::fun () {cout << "This is an inline function" << Endl;} int main () {Inlinec *in = new Inlinec; In->fun (); Delete in; return 0;}
Inline functions are similar to macros, copy, but he's safer than macros.
As an example:
#include <iostream>using namespace std; #define SUM (x) x*xclass inlinec{public:inline void Fun ();}; inline void Inlinec::fun () {cout << "This is an inline function" << Endl;} int main () {Inlinec *in = new Inlinec; In->fun (); Delete in; cout << "--------------------------------------" << Endl; cout << SUM (1+3) << Endl; return 0;}
Results:
650) this.width=650; "title=" 01.png "src=" https://s1.51cto.com/wyfs02/M01/95/AD/ Wkiom1kya1vwigobaaaj1jfy99y812.png-wh_500x0-wm_3-wmp_4-s_1380158475.png "alt=" Wkiom1kya1vwigobaaaj1jfy99y812.png-wh_50 "/>
And did not get results 16.
The rigor of the macro is not high.
Finally, a default: write the Declaration and implementation of the inline in the head.
This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1925645
C + + inline functions