使用默认参数调用函数

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef string::size_type sz;
 6 
 7 string screen(sz, sz, char = '#');
 8 string screen(sz, sz, char);
 9 
10 int main()
11 {
12     string windows = screen(6, 5);
13 
14     cout << windows << endl;
15     system("PAUSE");
16     return 0;
17 }
18 
19 string screen(sz a, sz b, char h) {
20     cout << a << endl;
21     cout << b << endl;
22     cout << h << endl;
23 
24     string s(a, h);
25 
26     return s;
27 }

第七行和第八行代码可以互换,不受影响,默认参数多的函数声明将另一个函数声明隐藏了。

对于第七第八行函数声明,可以对函数声明的形参加默认参数,但是不能修改,且默认参数在后面,如果在第一个,则后面的形参都需要默认参数值。

猜你喜欢

转载自www.cnblogs.com/Mayfly-nymph/p/9614876.html