一般情况,默认参数值在函数声明时声明的。因为在实用的程序中,函数声明总是与函数定义分离的,而在又有声明又有定义时,默认参数值自然只能置身于声明中。
以下例子是一般情况:
void point(int = 3, int = 4); //声明 默认参数值,同时注意可以形参省略
void point(int x, int y) { //定义 不允许再给出默认值
cout<<x<<endl;
cout<<y<<endl;
}
函数参数默认值只能从后往前设置,因为在进行实参与形参的匹配时是按从前到后的解析,如果默认参数是从后往前设置,那么有可能后面的参数没有设置默认值,在传实参的时候少传了没有默认值的实参,导致栈空间的变量没有被初始化,进而发生错误,例如:
void func(int a = 1, int b, int c, int d = 3); //错误的声明
func(2,3);//传入参数 a = 2? b = 2? c = 3?-----确定不了
相对的,默认实参只能从后往前逐个替换(即当实参从前往后替换完后),例如:
void func(int a, int b = 2, int c = 3, int d = 4); //正确声明
func(10, 15, 20, 30); //全部替换
func(); //错,a没有默认参数
func(12, 12); //替换了a和b
func(2, 15, ,20); //错,当从后替换默认参数时发现最后一个已经有了实参,c不能被替换,因为还有无默认参数的没被实参替换完的情况
默认参数值不能重复声明(这个很好理解了),例如:
void func(int a = 1);
void func(int a = 2); //错
分享到:
相关推荐
rules). - ADD: The property TFlexPanel.GridControl moved to the public section and is now available for writing (during the writing, the standard grid object TFlexGrid is released, and a user's one...
Learn about the new parameter passing rules, local variable scoping in blocks, fibers, and the new block declaration syntax, among other exciting new features. About Ruby 2.0 Ruby 2.0 is a minor ...
Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...
7.5.2.7 Explicit parameter type inferences 148 7.5.2.8 Exact inferences 149 7.5.2.9 Lower-bound inferences 149 7.5.2.10 Upper-bound inferences 150 7.5.2.11 Fixing 150 7.5.2.12 Inferred return type 150...
About_parameter.help.txt About_parsing.help.txt About_path_syntax.help.txt About_pipeline.help.txt About_property.help.txt About_provider.help.txt About_quoting_rules.help.txt About_redirection.help....
The defaultParameter value and optional Attributes 212 Implicitly Typed Local Variabl 212 Passing parameters by reference to a Method 214 Passing a variable Number of arguments to a Method 220 ...
- Main BIOS image is default flashing area if no any option present. - [/REBOOT], [/X], and [/S] will enable [/P] function automatically. - If [/B] present alone, there is only the Boot Block area ...
* Windows Make.rules.mak improved to work with and w/o openssl and zlib * Added --read-ssk-key option to pkcs15-tool (prints public key in ssh format) * use pkg-config for finding openct, add --enable...
2.8 Default and Named Arguments L1 26 2.9 Variable Arguments L1 26 2.10 Procedures 28 2.11 Lazy Values L1 28 2.12 Exceptions 29 Exercises 31 3 WORKING WITH ARRAYS A1 35 3.1 Fixed-Length Arrays 35 3.2 ...
xvii Contents Finishing Your Modules 154 Defining Module-Specific Errors 154 Choosing What to Export 155 Documenting Your Modules 156 Try It Out: Viewing Module Documentation ...
default, it will look for TASM.EXE. If you want to use a different assembler, use -E, as described in the Reference Guide. o When using THELP on an AT&T 6300, be sure to use the /L25 command-...
default, it will look for TASM.EXE. If you want to use a different assembler, use -E, as described in the Reference Guide. o When using THELP on an AT&T 6300, be sure to use the /L25 command-...
<END><br>91,Eurocalc.zip A Euro calculator - converts between all the Euro zone currencies according to the rules (single rate, 6 digit precision, 2 digit rounding) <END><br>92,smexec.zip This ...
2.2.1 Default Configuration and Images ................................................................. 8 2.2.2 Configuration With VxWorks Image Projects ................................................
From Wikipedia: Diplomatic protocol is commonly described as a set of international courtesy rules. These well-established and time-honored rules have made it easier for nations and people to live ...
Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................