函数的占位参数
C++中函数的形参可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:
返回值类型 函数名 (数据类型){}
#includeusing namespace std;void show(int a, int) //void show(int a,int = 10)//占位参数还可以有默认值,这时主函数里面可以不用赋值{cout<<"this is show" << endl;}int main() {show(10, 10);system ("pause");return 0;}
函数重载的注意事项:
1.引用作为重载的条件
#includeusing namespace std;void show(int &a) {cout<<"int &a" << endl;}void show(const int& a) {cout << "const int &a" << endl;}int main() {int a = 10;show(a);//这个a是可读可写的状态,默认show(int &a)//const会制造一个临时的数据,让&a指向临时的空间,合法,所以执行下面show(const int &a)show(10);system ("pause");return 0;}
2.函数重载碰到默认参数
#includeusing namespace std;void show(int a,int b = 10) {cout<<"show(int a, int b = 10)" << endl;}void show(int a) {cout << "show(int a)" << endl;}int main() {show(10);//此时出现了二义性,我们要尽量避免这种情况system ("pause");return 0;}