3. C ++ inline function, default parameter, placeholder parameter, 3.c inline
Main content of this chapter:
- 1)Inline functions (replacing macro code segments)
- 2)Default parameters
- 3)Placeholder Parameter
1. C ++Inline Function Analysis
1.1Before explaining inline functionsFirst, let's recall the definition of the define macro mentioned earlier:
As mentioned earlier, macro definitions will be replaced by Preprocessor text. The disadvantage is that there is no type check, no compilation process, and the compiler does not know what the type is.
Therefore, when a constant of A type is required in C ++, the const constant can be used to replace the macro constant, for example:
const int A=3; <---> #define A 3
1.2 If # define defines a macro code segment, the example is as follows:
#include <stdio.h>#define MAX(A,B) (A>B?A:B)int main(){int a=5;int b=4;printf("%d",MAX(++a,b)); }
Output result:
7
This is the disadvantage of macro definition, because the printf () above will be expanded:
printf("%d", ((++a) > b ? (++a) : b ) );
Therefore, a is added twice, so the output result is 7.
1.3 so C ++ adds inline functions to replace macro code segments
Inline functionsInlineKeywordsStatement, such as the MAX (A, B) macro above, can be replaced:
inline int MAX(int a, int b){return a > b ? a : b ;}
The difference between an inline function and a common function is as follows:
- Common functions:Before each call, the CPU will save the site (in the stack). After the call, additional overhead such as the site (Out stack) will be restored.
- Inline functions:The code segment in the inline function is expanded in the place of each call, saving additional overhead.
Note:When the code in the inline function is too large and the process is complex, the compiler may reject the inline request of the function and convert it into a common function.
2. Default Value of function parameters
The default value of parameters can be provided for functions in C ++. There are two main defining methods:
1) function declaration:
Int show (int x = 10); // declare the show function and define the parameter x. The default value is 10.
Int main () {show ();} int show (int x) {printf ("x = % d \ n", x );}
Output result:
x=10
2) When no function declaration is available:
Int show (int x = 10) // defines the parameter x. The default value is 10 {printf ("x = % d \ n", x);} int main () {show ();}
Output result:
x=10
2.1 when multiple default parameters exist
1) when defining, the default value of the parameter must be defined from right to left, for example:
Int show (int x, int y, int z = 10); // int show (int x, int y = 8, int z = 10 ); // The definition is correct. // int show (int x = 6, int y = 8, int z); // The definition is incorrect, no definition starting from right // int show (int x, int y = 8, int z); // definition error, not defined from right
2) When a function is called, the parameters are matched from left to right, for example:
#include <stdio.h>int show(int x, int y=8,int z=10); int main(){ show(6); //x=6,y=8,z=10; show(6,7); //x=6,y=7,z=10; show(6,7,8); //x=6,y=7,z=8; return 0;}int show(int x,int y,int z){printf("x=%d y=%d z=%d\n",x,y,z);}
Output result:
x=6 y=8 z=10x=6 y=7 z=10x=6 y=7 z=8
3. C ++Placeholder parameter of the Function
Placeholder parameter indicates that this function has only type declarations but no parameter names.
Note:This placeholder parameter cannot be used in the function body.
Instance 1:
Int func (int x, int); ...... func (); // call the func Function
Instance 2, which is used together with placeholder parameters and default parameters:
Int func (int = 0, int = 0 );... ...int main () {/* call the func function */func (); func (2); func (1, 2); return 0 ;}
From instance 2, we can see that the placeholder parameter and default parameter are mainly designed to be compatible with the code.
Next, let's learn: function overloading in 4.c++, C ++ Calling C Code, new/delete keyword, namespace (namespace)