#include <iostream>
using namespace std;
int add(int a, int b, int c = 2, int d = 3)
{
return a + b + c + d;
}
void main()
{
//cout << add(1) << endl; error,不默认的参数必须调用
cout << add(1, 2) << endl;
cin.get();
}
int sub(int a, int b, int c = 2, int d) //error:默认参数从右边开始,d是不默认的,必须放在c之前
{
return a + b + c;
}//参数进栈:从右到左
总结:
1、这是一种设计模式:外观模式
2、特点:函数参数可以默认调用,因此可以省略参数, cpp独有,C语言没有默认参数模式
3、默认参数从右边开始,参数进栈,从右到左
4、不默认的参数,都在左边
5、不默认的参数,必须传递
总结: