1 inline inline function
Why does C++ need inline functions? What is an inline function? What is the value of an inline function?
We already know that we can use const instead of macro definition for a constant, such as const int A=3; #define A 3
So for a function, can we also achieve such a macro? replacement effect.
Please see:
#define MYFUNC(a, b) ((a) < (b) ? (a) :(b)) // 宏替换的表示 inline int myfunc(int a, int b) //函数表示 { returna < b ? a : b; }
This is the inline function. Pay attention to the instructions
Instructions 1:
must inline int myfunc(int a, int b) and the implementation of the function body, written together
Explanation 2
The C++ compiler can compile a function inline
The function compiled inline by the C++ compiler is called an inline function
Inline functions are not defined in the final generated code
The C++ compiler directly inserts the function body at the place where the function is called
Inline functions do not have ordinary functions Extra overhead when calling (push on stack, jump, return) - existence value
Note 3: C++ compiler does not necessarily allow inlining requests for functions!
Explanation 4
Inline function is a special function with the characteristics of ordinary functions (parameter checking, return type, etc.)
Inline function is a special function for the compiler A request, so the compiler may reject this request
Inline functions are processed by the compiler, and the compiled function body is directly inserted into the calling place
Macro code snippets are preprocessed Processor, simple text replacement, without any compilation process
Note 5:
Modern C++ compilers can perform compilation optimization, so some functions even without inline declaration , may also be compiled inline by the compiler
In addition, some modern C++ compilers provide extended syntax that can force functions to be inline
For example: __attribute__((always_inline) in g++ )) Attribute
Explanation 6:
Limitations of inline compilation in C++:
There cannot be any form of loop statement
There cannot be too many conditional judgment statements
The function body cannot be too large
The address operation of the function cannot be performed
The inline declaration of the function must be before the calling statement
The compiler's restrictions on inline functions are not absolute. The advantage of inline functions over ordinary functions is that they save the overhead of pushing the stack, jumping and returning when the function is called.
Therefore, when the execution cost of the function body is much greater than the cost of pushing, jumping and returning, then inlining will be meaningless.
Conclusion:
1) Inline functions directly insert the function body into the place where the function is called during compilation
2) Inline is just a request , the compiler may not necessarily allow this request
3) Inline functions save the overhead of pushing on the stack, jumping and returning when calling ordinary functions
Second function Default parameters
In C++, you can provide a default value for the parameter when the function is declared (you need to call me according to the value I provide you)
The value of this parameter is not specified when the function is called. , the compiler will automatically replace it with the default value
void myPrint(int x = 3) { printf("x:%d", x); }
Rules for function default parameters
Only parameters at the end of the parameter list can provide default parameter values
Once you start using the default parameter value in a function call, all parameters after this parameter must use the default parameter value
void printAB(int x = 3) { printf("x:%d\n",x); }
In the default parameter rule, if the default parameter appears, Then everything on the right must have default parameters
void printABC(int a, int b, int x = 3, inty=4, int z = 5) { printf("x:%d\n",x); } int main62(int argc, char *argv[]) { printAB(2); printAB(); system("pause"); return0; }
3. Function placeholder parameters (take a pit first)
placeholder parameters only have parameter type declarations, but no parameter names Statement
Generally, placeholder parameters cannot be used inside the function body
int func(int a, int b, int ) { returna + b; } int main01() { //func(1, 2); //可以吗?错误 printf("func(1,2, 3) = %d\n", func(1, 2, 3)); getchar(); return0; }
4. Default parameters and placeholder parameters (combination)
You can combine placeholder parameters with default parameters. Significance: Leave clues for future program expansion. Compatible with irregular writing methods that may appear in C language programs
int func2(int a, int b, int = 0) { return a + b; } void main() { //如果默认参数和占位参数在一起,都能调用起来 func2(1,2); func2(1,2, 3); system("pause"); }
结论://如果默认参数和占位参数在一起,都能调用起来
五.函数重载(重点)
1 )函数重载概念
函数重载(FunctionOverload)
用同一个函数名定义不同的函数
当函数名和不同的参数搭配时函数的含义不同
2 )函数重载的判断标准
函数重载至少满足下面的一个条件:
参数个数不同
参数类型不同
参数顺序不同
3 )函数返回值不是函数重载的判断标准
4)编译器调用重载函数的准则
将所有同名函数作为候选者
尝试寻找可行的候选函数
精确匹配实参
通过默认参数能够匹配实参
通过默认类型转换匹配实参
匹配失败
最终寻找到的可行候选函数不唯一,则出现二义性,编译失败。
无法匹配所有候选者,函数未定义,编译失败。
5)函数重载的注意事项
重载函数在本质上是相互独立的不同函数(静态链编)
重载函数的函数类型是不同的
函数返回值不能作为函数重载的依据
函数重载是由函数名和参数列表决定的。
六. 函数重载与函数指针的结合
函数重载与函数指针
当使用重载函数名对函数指针进行赋值时
根据重载规则挑选与函数指针参数列表一致的候选者
严格匹配候选者的函数类型与函数指针的函数类型
//函数指针 基础的语法
//1声明一个函数类型
typedef void (myTypeFunc)(int a,int b) ; //int
//myTypeFunc *myfuncp = NULL; //定义一个函数指针 这个指针指向函数的入口地址
//声明一个函数指针类型
typedef void (*myPTypeFunc)(int a,int b) ; //声明了一个指针的数据类型
//myPTypeFunc fp = NULL; //通过 函数指针类型 定义了 一个函数指针 ,
//定义一个函数指针 变量
void (*myVarPFunc)(int a, int b);
//
待补充!
以上就是C++复习要点总结之二——函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!