`
swincle
  • 浏览: 78064 次
  • 性别: 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);   //错
 
分享到:
评论

相关推荐

    .net Url重写示例

    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } ``` 这个默认路由将映射URL到相应的控制器和动作,如"/Home/Index"。 除了基本的URL重写,还可以使用...

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

    ASP.NET中的伪静态

    new { controller = "Article", action = "Details", id = UrlParameter.Optional, title = UrlParameter.Optional } ); } ``` 在这个例子中,`文章/{id}/{title}` 是路由模板,映射到`ArticleController`的`...

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

    URLRewrite

    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 ); } ``` 5. **URL重写最佳实践** - **保持可读性**:重写后的URL应简洁、有意义,方便用户理解页面内容。 - **...

    C#实现伪静态

    new { controller = "Product", action = "Details", productId = UrlParameter.Optional } ); } ``` 这里的路由规则会将 `products/{productId}` 映射到 `ProductController` 的 `Details` 动作。 3. ASP.NET ...

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

    Google_C++_Style_Guide

    6. **Function Parameter Ordering**:参数通常按照使用频率排序,最常使用的参数放在前面。 7. **Names and Order of Includes**:标准库头文件优先,然后是第三方库,最后是项目内部头文件。 8. **Scoping**:...

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

    zwt实现伪静态网页案例

    如`routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional })`。 - 控制器中的动作方法会根据路由规则处理请求,返回视图,视图...

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

    Road_vehicles_UDS_ISO14229-1_Part1 - Copy.pdf

    UDS协议定义了不同的诊断会话类型,如默认会话(Default Session)、编程会话(Programming Session)等,每种会话类型允许执行不同的诊断服务。 9. 负面响应码(Negative response codes) 为了对不符合要求或无法...

    如何命名URL

    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 ); } ``` 这个路由会将`home/index`解析到`HomeController`的`Index`动作。 4. **JavaScript、CSS与HTML**:前端方面...

    谷歌_C++编码风格指南

    - **函数参数次序(Function Parameter Ordering)**:参数通常按照“常量引用、指针、基本类型”的顺序排列。 - **包含的命名和次序(Names and Order of includes)**:标准库先于项目库,项目库先于本地头文件。 2...

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

    Google对外发布C++编码规范

    - **Function Parameter Ordering**:函数参数的顺序应该直观且易于理解。通常按照“最常变化的参数”到“最少变化的参数”的顺序排列。 - **Names and Order of Includes**:头文件的包含应该按照特定的顺序进行,...

    c 编码规范

    - **Function Parameter Ordering**:参数的顺序应当直观并便于理解。一般将最常用的参数放在前面。 - **Names and Order of Includes**:头文件的命名应反映其内容,同时,包含这些头文件的顺序也有规定,通常先...

    Google C++ Style Guide 3.180版(最新版)

    - **Function Parameter Ordering**:函数参数的顺序应该符合逻辑,易于理解和记忆,比如先传递对象再传递操作该对象的参数。 - **Names and Order of Includes**:包括头文件时应遵循一定顺序,例如先标准库后项目...

    Google C++ 编程规范(精心制作的链接文档-方便查看学习)

    - **Function Parameter Ordering**:参数的顺序应当按照逻辑或使用频率进行排列,使得最常用的参数位于前面。 - **Names and Order of Includes**:头文件应该按特定顺序包含,通常先包含标准库头文件,然后是第...

Global site tag (gtag.js) - Google Analytics