The Inline keyword is used to define an inline function of a class. The main reason for introducing it is to use it to replace the macro definition in expression form in C.
An example of macro definition in expression form:
# Define expressionname (var1, var2) (var1) + (var2) * (var1)-(var2 ))
1. Why macro in expression format?
1). High macro execution efficiency. Because it is directly replaced by text at the position required in the Code, there are no parameter pressure stacks, code generation and other operations, which are completed by the pre-processor.
2) function calls require saving and restoring the site, with a certain amount of time and space overhead.
2. disadvantages of Macros in Expression Form
1). Macros cannot access the private and protected members of objects.
2) The macro cannot check the parameter validity, and the return value cannot be forcibly converted.
3). It is easy to produce Ambiguity
3. Advantages of inline
1). High Efficiency similar to macro
2). An inline function is a real function that can access the private and protected members of an object. The compiler can check its parameter validity.
4. When to use inline
1). macro definition in expression form can be completely replaced
2). When the function content is very simple and requires a large number of repeated calls
5. inline usage
1). inline functions must be declared together with the function body to be valid inline int add (int A, int B) {return a + B };
2). In C ++, the function of the function body is defined inside the class and is considered as an inline function by default.
3) inline functions are the most widely used in C ++ classes and should be used to define access functions for private members.
Have you said this today?