`
swincle
  • 浏览: 76857 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Default parameter rules

    博客分类:
  • C++
 
阅读更多

一般情况,默认参数值在函数声明时声明的。因为在实用的程序中,函数声明总是与函数定义分离的,而在又有声明又有定义时,默认参数值自然只能置身于声明中。

以下例子是一般情况:

 

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);   //错
 
分享到:
评论

相关推荐

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    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...

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    C# 语言规格说明(English Edition第五版)

    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...

    PowerShellPack

    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....

    CLR via C# 第4版 英文PDF

    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 ...

    afuwin.rar

    - 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 ...

    opensc-0.12.0.tar.gz

    * 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...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    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 ...

    Beginning Python (2005).pdf

    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 ...

    Turbo C 2.01[DISK]

    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-...

    Turbo C 2.00[DISK]

    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-...

    VB编程资源大全(英文源码 其它)

    &lt;END&gt;&lt;br&gt;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) &lt;END&gt;&lt;br&gt;92,smexec.zip This ...

    vxworks_kernel_programmers_guide_6.9

    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 ...

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................

Global site tag (gtag.js) - Google Analytics