为什么应该在C ++函数中最后添加默认参数? [英] Why should default parameters be added last in C++ functions?

查看:643
本文介绍了为什么应该在C ++函数中最后添加默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要在C ++函数中最后添加默认参数?

Why should default parameters be added last in C++ functions?

推荐答案

简化语言定义并保持代码可读性。 / p>

To simplify the language definition and keep code readable.

void foo(int x = 2, int y);

要调用并利用默认值,您需要这样的语法: p>

To call that and take advantage of the default value, you'd need syntax like this:

foo(, 3);

这可能觉得太奇怪了。另一个选择是在参数列表中指定名称:

Which was probably felt to be too weird. Another alternative is specifying names in the argument list:

foo(y : 3);

必须使用新的符号,因为这已经表示:

A new symbol would have to be used because this already means something:

foo(y = 3); // assign 3 to y and then pass y to foo.

命名方法被ISO委员会拒绝考虑,因为他们不愿意引入一个新的意义

The naming approach was considered by rejected by the ISO committee because they were uncomfortable with introducing a new significance to parameter names outside of the function definition.

如果您对更多的C ++设计理念感兴趣,请阅读 Stroustrup的C ++的设计和演变

If you're interested in more C++ design rationales, read The Design and Evolution of C++ by Stroustrup.

这篇关于为什么应该在C ++函数中最后添加默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆