- if a parameter has a default argument, all the parameters that follow it must also have default arguments.
- those least likely to use a default value appear first and those most likely to use a default appear last. (因为顺着可以忽略,倒着没法忽略)
Default Argument Declarations
最好就一次,但你要真来多次,那么请遵守:已有的default不能变1,可以增加default
string screen(int, int, char = ' ');
string screen(int, int, char = '*'); // error: redeclaration
string screen(int = 24, int = 80, char); // ok: add default arug
看最后一个好像很奇怪,怎么感觉违反了第一条?其实不是,这里沿用了之前的default
Default Argument Initializers
其实已有的default也可以变,只要用跟function decalration处在同一个scope的变量来default就行
int wd = 80;
char def = ' ';
int ht();
string screen(int = ht(), int = wd, char = def);
string window = screen(); // calls screen(ht(), 80, ' ')
void f2(){
def = '*'; // changes the default
sz wd = 100; // hide
window = screen(); // calls screen(ht(), 80, '*')
}