一、内联函数
c 语言中有宏函数的概念。宏函数的特点是内嵌到调用代码中去,避免了函数调用 的开销。但是由于宏函数的处理发生在预处理阶段,缺失了语法检测 和有可能带来的语 意差错。
特点:
1)内联函数声明时inline关键字必须和函数定义结合在一起,否则编译器会直接忽略内联请求。
2)C++编译器直接将函数体插入在函数调用的地方 。
3)内联函数没有普通函数调用时的额外开销(压栈,跳转,返回)。
4)内联函数是一种特殊的函数,具有普通函数的特征(参数检查,返回类型等)。
5) 内联函数由 编译器处理,直接将编译后的函数体插入调用的地方,宏代码片段 由预处理器处理, 进行简单的文本替换,没有任何编译过程。
6)C++中内联编译的限制:
不能存在任何形式的循环语句
不能存在过多的条件判断语句
函数体不能过于庞大
不能对函数进行取址操作
函数内联声明必须在调用语句之前
7)编译器对于内联函数的限制并不是绝对的,内联函数相对于普通函数的优势只是省去了函数调用时压栈,跳转和返回的开销。因此,当函数体的执行开 销远大于压栈,跳转和返回所用的开销时,那么内联将无意义。
内联函数总结:
优点:避免调用时的额外开销(入栈与出栈操作)
代价:由于内联函数的函数体在代码段中会出现多个“副本”,因此会增加代码段的空间。
本质:以牺牲代码段空间为代价,提高程序的运行时间的效率。
适用场景:函数体很“小”,且被“频繁”调用。
#if 0
#include<iostream>
using namespace std;//代码比较少时才采用inline函数
//若代码比较多,有复杂业务时即使申明为inline函数,也不会采用inline函数的执行方式
void PrintAB1(int a, int b) {cout << "a=" << a << endl;cout << "b=" << b << endl;
}
void test01() {int a = 10;int b = 10;for (int i = 0; i < 1000; i++){a++;b++;PrintAB1(a, b); //函数压栈 a,b压栈,形参拷贝实参,执行语句,返回值再出栈 有一定的空间开销}
}//宏函数 解决了函数压栈 出栈 但解决不了参数的替换问题(简单的参数拷贝)
#define MAX(a,b) \(a)>(b)?(a):(b)int max(int a, int b) {return (a > b) ? a : b;
}void test02() {int a = 20;int b = 10;int c = 0;c = MAX(a, b);cout << c << endl; //20c = MAX(a++, b++); //(a++)>(b++)?(a++):(b++) 宏函数没有语法检测能力,预处理时执行cout << c << endl; //21c = max(a++, b++);cout << c << endl; //22
}inline void PrintAB2(int a, int b) {cout << "a=" << a << endl;cout << "b=" << b << endl;
}void test03() {int a = 10;int b = 10;for (int i = 0; i < 1000; i++){a++;b++;PrintAB2(a, b); //相当于将函数展开}
}
inline void PrintAB3(int a, int b); //在申明是使用inline,但是函数体没有加inline 还是普通函数void PrintAB3(int a, int b){cout << "a=" << a << endl;cout << "b=" << b << endl;
}int main() {test02();
}
#endif
二、函数的默认参数和占位参数
1、默认参数
通常情况下,函数在调用时,形参从实参那里取得值。对于多次调用用一函数同一 实参时,C++给出了更简单的处理办法。给形参以默认值,这样就不用从 实参那里取值了。
在默认参数规则 ,如果默认参数出现,那么右边的都必须有默认参数 存在默认参数时, 当置于右侧。
默认参数规则:
只有参数列表后面部分的参数才可以提供默认参数值
一旦在一个函数调用中开始使用默认参数值,那么这个参数后的所有参 数都必须使用默认参数值
2、占位参数
函数占位参数
占位参数只有参数类型声明,⽽没有参数名声明
⼀般情况下,在函数体内部⽆法使⽤占位参数
#include<iostream>
using namespace std;//1、默认参数
void func1(int a) {cout << "a=" << a << endl;
}void func2(int a = 666) {cout << "a=" << a<<endl;
}
void test01() {int a = 200;func1(a); //200//func1(); //编译错误func2(a); //200func2(); //正确 666
}
int get_volume1(int len, int width, int height) {cout << "len=" << len << endl;cout << "width=" << width << endl;cout << "height=" << height << endl;return len*width*height;
}
int get_volume2(int len, int width, int height=1) { //左边有默认值时,右边必须有默认值cout << "len=" << len << endl;cout << "width=" << width << endl;cout << "height=" << height << endl;return len*width*height;
}
void test02() {int len = 10;int w = 20;int h = 30;cout << "体积是:"<<get_volume1(len, w, h) << endl; //6000cout << "体积是:" << get_volume2(len, w) << endl; //200 从左向右对应参数
}//2、占位参数
void funn1(int x) {cout << "x=" << x << endl;
}
void funn2(int x, int);
void funn2(int x,int) { //第二个参数无意义,但必须传递值 cout << "x=" << x << endl;
}
void funn3(int x, int = 0) { //第二个参数无意义,但必须传递值 cout << "x=" << x << endl;
}
void test03() {int a = 10;funn1(a);funn2(10, 20);//funn2(10); //错误funn3(10, 20);funn3(10); //正确
}
int main() {//test01();test02();
}
三、函数重载
1、函数重载(Function Overload):用同一个函数名定义不同的函数,当函数名和不同的参数搭配时函数的含义不同
2、重载规则(参数个数不同 参数类型不同 参数顺序不同):(前提是在同一作用域之内)
1,函数名相同。
2,参数个数不同,参数的类型不同,参数顺序不同,均可构成重载。
3,返回值类型不同则不可以构成重载。 仅返回值类型不同,不是重载。
3、调用规则
1,严格匹配,找到则调用。
2,通过隐式转换寻求一个匹配,找到则调用。
4、编译器调用重载函数的准则:
1.将所有同名函数作为候选者
2.尝试寻找可行的候选函数
3.精确匹配实参
4.通过默认参数能够匹配实参
5.通过默认类型转换匹配实参
6.匹配失败
7.最终寻找到的可行候选函数不唯一,则出现二义性,编译失败。
8.无法匹配所有候选者,函数未定义,编译失败。
5、重载底层实现(name mangling)
C++利用 name mangling(倾轧)技术,来改名函数名,区分参数不同的同名函数。 实现原理:用 v c i f l d 表示 void char int float long double 及其引 用。
void func(char a); // func_c(char a)
void func(char a, int b, double c); //func_cid(char a, int b, double c)
6、函数重载与函数默认参数
一个函数,不能既作重载,又作默认参数的函数。当你少写一个参数时,系统无法确认是重载还是默认参数。
#if 1
#include<iostream>
using namespace std;//函数的返回值 函数的形参列表(参数个数,参数类型,参数顺序)
/*
int func(int a, int b) {//...
}
*/
//c语言中只要函数名相同 无法通过编译
//1、c++中增加了函数重载:函数名相同,形参列表不同
//若函数名相同,形参列表相同,函数返回值不同,则不可以通过编译
//2、函数返回值并不是构成函数重载的条件
//3、如果有函数重载,不要写默认参数,为了避免调用出现函数冲突//调用规则
// <1>如果能够严格匹配调用完全匹配
// <2>如果没有完全匹配,调用隐式转换
// <3>如果都匹配不到,调用失败
int func1(int a, int b) {cout << "func1(int a, int b) " << endl;return 0;
}
int func1(int a, char b) {cout << "func1(int a, char b)" << endl;return 0;
}
/*
void func1(int a, char b) {cout << "func2" << endl;
}
*/
void test01() {func1(1, 2); //func1(int a, int b)func1(1, 'a'); //func1(int a, char b)
}int func2(int a, char b) {cout << "func2(int a, char b)" << endl;return 0;
}
int func2(int a, char b, int c = 0) { //等价于int func2(int a, char b, int=0) {cout << "func2(int a, char b, int c = 0)" << endl;return 0;
}
//如果有函数重载,不要写默认参数,为了避免调用出现函数冲突
void test02() {//func2(1, 'a'); 编译错误 编译器不会选择func2(1, 'a', 22);
}int func3(int a, char b) {cout << "func3(int a, char b)" << endl;return 0;
}
int func3(int a, char b, int c) { //等价于int func3(int a, char b, int) {cout << "func3(int a, char b, int c)" << endl;return 0;
}void test03() {func3(1, 'a'); func3(1, 'a', 22);
}void print1(int a) {cout << "print1(int a)" << " a=" << a << endl;
}
void print1(double a) {cout << "print1(double a)" <<" a=" << a << endl;
}
/*
void print1(char a) {cout << "print1(char a)" << " a=" << a << endl;
}*/
void test04() {print1(10); //print1(int a) a = 10print1(1.2); //print1(double a) a = 1.2print1('A'); //print1(int a) a = 65print1(3.1f); //print1(double a) a=3.1 隐式转换//print1("ter"); 报错没有匹配
}
int main() {//test01();//test02();//test03();test04();return 0;
}
#endif
7、函数重载和函数指针结合
函数重载与函数指针
当使⽤重载函数名对函数指针进⾏赋值时
根据重载规则挑选与函数指针参数列表⼀致的候选者
严格匹配候选者的函数类型与函数指针的函数类型
8、函数指针基本语法
//⽅法⼀:
//声明⼀个函数类型
typedef void (myTypeFunc)(int a,int b);
//定义⼀个函数指针
myTypeFunc *myfuncp = NULL; //定义⼀个函数指针 这个指针指向函数的⼊⼝地址
//⽅法⼆:
//声明⼀个函数指针类型
typedef void (*myPTypeFunc)(int a,int b) ; //声明了⼀个指针的数据类型
//定义⼀个函数指针
myPTypeFunc fp = NULL; //通过 函数指针类型 定义了 ⼀个函数指针 ,
//⽅法三:
//定义⼀个函数指针 变量
void (*myVarPFunc)(int a, int b);
#include<iostream>
using namespace std;int func(int a, int b) {cout << "func" << endl;return 0;
}//1、定义一种函数类型
typedef int(MY_FUNC)(int, int); //第一个int为返回值类型 //(int, int)为参数列表 //MY_FUNC为函数类型名
void test01() {//1.MY_FUNC *fp = NULL;fp = func;fp(10, 20); //func(*fp)(10, 20); //func
}//2、定义一个指向函数类型的指针类型
typedef int(*MY_FUNC_P)(int, int); //MY_FUNC_P为指向int func(int a, int b) 函数类型的指针类型名
void test02() {MY_FUNC_P fp = NULL;fp = func;fp(10, 20); //func
}//3、通过函数类型直接定义 常用
void test03() {int(*fp3)(int, int) = NULL;fp3 = func;fp3(100, 200); //func
}
int funcc(int a, int b) {cout << "funcc(int a, int b)" << endl;return 0;
}
int funcc(int a, int b,int c) {cout << "funcc(int a, int b,int c)" << endl;return 0;
}
void test04() {int(*fp3)(int, int) = NULL;fp3 = funcc; //fp3-->funcc(int a, int b)fp3(100, 200); //funcc(int a, int b)//fp3(10, 20, 30); 编译错误 函数重载和函数指针重载是两回事
}//实际上在给函数指针赋值的时候 是会发生函数重载匹配的
//在调用函数指针时,所调用的函数就已经是固定的了
int main() {//test01();//test02();//test03();test04();return 0;
}
函数重载总结:
重载函数在本质上是相互独立的不同函数。
函数的函数类型是不同的
函数返回值不能作为函数重载的依据
函数重载是由函数名和参数列表决定的。