函数的默认参数
在c++中,函数的形参列表中的形参是可以有默认值的。
语法:例如:void func(int a, int b = 10)
#include <iostream>
using namespace std;
int text(int a, int b = 10, int c = 20)
{
return a + b + c;
}
int main()
{
cout << text(0) << endl; //30
cout << text(0, 20) << endl; //40
cout << text(0, 20, 40) << endl; //60
return 0;
}
默认参数规则是:如果我们自己传入数据,就会调用我们传入的数据,没有的话,就会调用默认参数。
注意事项:
1:如果某个位置已经有了默认参数,那么这个位置之后的参数都应该有默认参数。
#include <iostream>
using namespace std;
int text(int a, int b = 10, int c = 20)
{
return a + b + c;
}
//int text1(int a, int b = 10, int c)
//{
// return a + b + c;
//}
int main()
{
cout << text(0) << endl; //30
cout << text(0, 20) << endl; //40
cout << text(0, 20, 40) << endl; //60
// cout << text1(0, 20) << endl;
return 0;
}
之所以要求这样,我自己觉得,如果参数列表为(int a, int b = 10, int c),再传入(0, 20),因为默认参数本身是不需要传入数据的,但我在第二个位置传入了数据,编译器就不知道这个数据是应该替换掉b,还是给c赋值。而且如果函数有许多参数的话,也会使得代码很混乱。
//int text1(int a = 10, int b, int c = 20, int d, int e = 30, int f, int g = 40)
//{
// return a + b + c + d + e + f + g;
//}
int main()
{
// cout << text1(10, 30, 40);
return 0;
}
看着就很混乱
2:如果声明有默认参数,实现不能有,实现有默认参数,声明不能有。
#include <iostream>
//int text(int a, int b = 10, int c = 20);
int text(int a, int b, int c);
int main()
{
cout << text(0) << endl; //30
cout << text(0, 20) << endl; //40
cout << text(0, 20, 40) << endl; //60
return 0;
}
int text(int a, int b = 10, int c = 20)
{
return a + b + c;
}
其原因在于,如果声明和实现都有默认参数,当两个默认参数不一致时,编译器就不知道用哪个参数,就会产生二义性。
函数的占位参数
语法:void func(int a, int )
#include <iostream>
void text(int a, int)
{
cout << "this func" << endl;
}
int main()
{
text(10, 20);
return 0;
}
函数的占位参数在传参时,必须传入数值。
当函数占位参数有默认参数时,可以不传入数值。
#include <iostream>
void text(int a, int = 10)
{
cout << "this func" << endl;
}
int main()
{
text(10);
return 0;
}
函数重载
作用:函数名可以相同,提高复用性。
函数重载满足条件:
- 在同一个作用域下
- 函数名相同
- 函数参数类型不同,或者个数不同,顺序不同
注意:函数返回值不可以作为函数重载条件。(函数重载发生在函数调用时,无法通过返回值来判断,只能通过传入的参数判断)
#include <iostream>
using namespace std;
void func()
{
cout << "func的调用" << endl;
}
void func(int a) //参数个数不同
{
cout << "func(int a)的调用" << endl;
}
//int func(int a) //函数返回值不同不可以作为重载条件
//{
// return a;
//}
void func(double a) //参数类型不同
{
cout << "func(double a)的调用" << endl;
}
void func(int a, double b)
{
cout << "func(int a, double b)" << endl;
}
void func(double a, int b) //参数顺序不同
{
cout << "func(double a, int b)" << endl;
}
int main()
{
func();
func(10);
func(3.14);
func(10, 3.14);
func(3.14, 10);
return 0;
}
函数重载注意事项
1:引用作为重载条件
首先要注意的是:引用不能作为函数重载条件。
如下面代码,编译器是会报错的。
#include <iostream>
using namespace std;
void func(int a)
{
cout << "func(int a)" << endl;
}
void func(int& a)
{
cout << "func(int &a)" << endl;
}
int main()
{
int a = 10;
func(a);
return 0;
}
当引用作为函数参数const作为重载条件时
#include <iostream>
using namespace std;
//void func(int a)
//{
// cout << "func(int a)" << endl;
//}
void func(int& a)
{
printf("func(int& a)\n");
}
void func(const int& a)
{
printf("func(const int& a)\n");
}
int main()
{
int a = 10;
func(a);
const int b = 10;
func(b);
func(10);
return 0;
}
程序运行知道,常量和const修饰的变量会调用func(const int& a),而变量会调用func(int &a);
原因是一个指针不能指向一个常数,但常量指针可以。
但没有引用修饰函数参数时,const不能作为重载条件。
#include <iostream>
using namespace std;
void func(int a)
{
cout << "func(int a)" << endl;
}
void func(const int a)
{
cout << "func(const int a)" << endl;
}
//void func(int& a)
//{
// printf("func(int& a)\n");
//}
//
//void func(const int& a)
//{
// printf("func(const int& a)\n");
//}
int main()
{
int a = 10;
func(a);
const int b = 10;
func(b);
func(10);
return 0;
}
编译器在执行上面代码时,会报重复定义函数的错误。
总结:
- 两函数都没有const修饰时,有无引用不能作为重载条件。
- 两函数都没有引用时,有无const修饰不可作为重载条件
- 两函数都有引用时有无const修饰可作为重载条件。
原因:
没有const修饰的的引用是个指针常量,只能指向一个变量。
有const修饰的引用是常量指针,可以指向一个常量。
所以对于1,传入变量时不能发生函数重载,但传入常量或const变量可以发生重载
对于2,由于没有涉及指针,两函数只是单纯的值传递可以变量常量都可以传入,所以不能发生函数重载。
能否传入函数 | int | const int | int & | const int & |
int a; | √ | √ | √ | √ |
const int b; | √ | √ | × | √ |
10; | √ | √ | × | √ |
2:函数重载碰上默认参数
#include <iostream>
void text(int a, int b = 10)
{
cout << "text(int a, int b = 10)" << endl;
}
void text(int a)
{
cout << "text(int a)" << endl;
}
int main()
{
text(10); //不能发生重载
text(10, 20); //可以发送重载
return 0;
}
在第一次调用时,两个函数的语法都适用,编译器不知道调用哪个函数,也就无法发生函数重载,在第二次调用时,调用只适用于第二个函数,就可以发生函数重载。
总结来看,能否发生函数重载主要在于:传入的参数是否和函数的参数相匹配,两个参数不相匹配的同名函数才能发生重载,例如参数为const int &a的,(只能接受常量和const变量),就可以和参数为int &a(既能接受常量也能接受变量)的函数重载。
函数重载情况多样,但只要明白原理,很多时候也能自己判断。