占位参数:
#include <iostream>
#include <string>
using namespace std;
void func(int a, int) {
cout << " this is func" << endl;
}
int main() {
func(10, 10);
system("pause");
}
函数重载
作用:函数名可以相同,提高复用性。
函数重载满足条件:
- 同一个作用域下
- 函数名相同
- 函数参数类型不同 或者个数不同 或者顺序不同
注意: 函数的返回值不可以作为函数重载的条件
//注意事项: 函数的返回值不可以作为函数重载的条件
void func(double a, int b)
{
cout << " 重载" << endl;
}
int func(double a, int b) //这就报错了
{
cout << " 重载!!!" << endl;
}
引用作为函数的重载的条件
void func(int &a)
{
cout << "func(int &a)的调用" << endl;
}
void func(const int& a) //他们属于类型不同 ,所以重载语法正确 const是一个只读状态
{
cout << "func(int &a)的调用" << endl;
}
int main() {
//func();
int a = 10;
func(a); //a 是一个变量,可读可写。所以调的是"func(int &a)的调用"
func(10); //变成了调用下一个版本
//其实,int &a=10 这是不合法的。
system("pause");
}