参考博客:https://blog.csdn.net/long0624/article/details/80066898
缺省参数分为:全缺省参数和办缺省参数
全缺省参数:C语言中,函数没有指定参数列表时,默认可以接收任意多个参数。而C++中,因为严格的参数类型检测,没有参数列表的函数默认为void,不接收任何参数。 缺省参数是在声明或定义函数时为函数的参数指定一个默认值。(注意这边的缺省参数是有默认值的)
全缺省参数:
void test(int i = 1,int j=2,int k=3)
{
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;
}
半缺省函数:
void test(int a,int b=2,int c=3)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
这种情况下传递给的只有b、c的值,则在main函数中:
main函数中,需要将原先的位置
int main()
{
test(1,2);
}
所以最后的值就是将原先的值,进行赋值。
得到最后的值为:1,2,3