I. Inline functions
1. Defining inline functions within a class declaration
2. Inline functions
In C + +, users can create short functions that are not actually called, and their code is expanded in each invocation of the program line. This process is similar to macros that use similar functions. In order for a function to expand code in the program line without being called, simply precede the function with the keyword inline.
For example, in the following program, the function max () extends within a row without being called:
#include <iostream>using namespace std;inline int max (int a, int b) {return a>b?a:b;} int main () {Cout<<max (10,20); cout<< "" <<max (99,88);
The above program is equivalent to the following program:
#include <iostream>using namespace Std;int main () {cout<< (10>20?10:20); cout<< "" << (99>88>99:88);}
Inline functions are an important addition to C + + because they enable programmers to write very effective code. Because classes generally require several interface functions that are often executed, the efficiency of these functions is very important in C + +. We know that each time the function is called, the variable is going to the stack, the various register contents are saved, and the function returns and restores their contents. The problem is that these instructions take time. However, if a function expands within a row, those actions do not exist. Of course, while the function inline extension can produce faster speeds, it is best to inline only those functions that significantly affect the performance of the program because of the longer code generated by repeated coding.
Inline is a request to the compiler, not a command. The compiler can choose to ignore it. Also, some compilers cannot inline all types of functions. For example, a compiler cannot normally inline a recursive function. You must consult your own compiler user manual to understand the limitations of inline. If a function cannot be inline, it is treated as a normal function call.
The inline keyword is not part of C + + 's subset, so C89 does not define it, however, it is added in C99.
An inline function can be a member of a class.
Class myclass{int a,b;public:void init (int i,int j); void Shou ();}; inline void Myclass::init (int i,int j) {a = i; b = j;} inline void Myclass::show () {cout<<a<< "" <<b<< "\ n";}
To define an inline function within a class declaration
It is possible to define an inline function within a class declaration (within curly braces). If a function is defined within a class declaration, it is automatically converted to an inline function (if possible). There is no need (but not an error) to precede the function declaration with the keyword inline.
Class myclass{int a,b;public://automatic-inline void init (int i, int j) {a=i;b=j;} void Show () {cout<<a<< "" <<b<< "\ n";}}
Constructors and destructors can also be inline.
Transferred from: http://zhidao.baidu.com/link?url=C_ svpmozat0wuft8zuxfio6zr6tfu958ug-7r7tlvgv4biekemwgg0ldv05sb2uvxijmhy3zy1da1hm3fgbeghxphl6zjvvpbdclwtyx39w
This article is from the "Squirrel" blog, please be sure to keep this source http://apinetree.blog.51cto.com/714152/1599464
C + + Learning path-(1. Review Class)