为什么这个声明了两个参数的构造函数可以只用一个参数来调用呢?
我在大学观看了讲师的视频,他谈到 Rational 类时,其构造函数是这样的:
Rational (int top=0 , int bottom=1)
: t(top) , b(bottom) {normalize();}
到目前为止,一切都很好,但是!他还说,您只能使用 1 个参数(顶部参数)调用构造函数,并且因为底部初始化为值 1,所以有理数例如:Rational(3)
将是 3/1 。
但 !!我想知道如何仅在仅支持 2 个参数的情况下使用具有 1 个值的构造函数?
我知道在java中,如果我们有x个构造函数接收的参数(考虑没有其他构造函数并且x>0),我们必须将它们全部传输而不是1而不是2...
请帮我解决这场冲突...
thnx...
I watched my lecturer's video from my university and he says about the Rational class that its constructor goes like this:
Rational (int top=0 , int bottom=1)
: t(top) , b(bottom) {normalize();}
Until now everything is OK, BUT !! he also said that you can call the constructor only with 1 argument (top argument) and because that bottom initialize to value of 1 the rational for example: Rational(3)
will be 3/1.
BUT !! I wonder how can we use a constructor with 1 value only if it supports 2 arguments ONLY ?
I know that in java if we have x number of argument that the constructor receive (consider no other constructor and x>0) we must transfer them all not 1 instead of 2...
Please help me solve this conflict...
thnx...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
构造函数声明中的
=
给出参数默认值。如果您在调用构造函数时没有自己提供值,编译器将为您填写声明的默认值。构造函数不会知道其中的区别 - 它会看到两个参数,并且无法检测调用者是否提供了这两个值或编译器是否填充了其中一些值,但这通常是可以的。 (如果您需要知道差异,请声明多个构造函数,每个构造函数都有不同数量的参数,并且没有默认值。)甚至可以使用无参数调用您的构造函数,因为它们都有默认值价值观。在这种情况下,第一个参数的默认值为 0,第二个参数的默认值为 1。
参数值只能从末尾省略。也就是说,您不能在不省略
bottom
参数的情况下省略top
参数。您给出的第一个实际参数将始终对应于声明中的第一个形式参数。同样,默认参数只能从最后开始定义。如果没有为bottom
声明一个默认参数,则无法为top
定义默认参数。The
=
in the constructor declaration give the parameters default values. If you don't provide a value yourself when you call the constructor, the compiler will fill in the declared default value for you. The constructor won't know the difference — it will see both parameters, and it will have no way of detecting whether the caller provided both values or whether the compiler filled in some of them, but that's usually OK. (If you need to know the difference, then declare multiple constructors, each with a distinct number of parameters, and no default values.)Your constructor can even be called with no parameters because both of them have default values. In that case, the first parameter will have the default value 0 and the second will get 1.
Parameter values can only be omitted from the end. That is, you cannot omit the
top
parameter without also leaving out thebottom
parameter. The first actual parameter you give will always correspond to the first formal parameter in the declaration. Likewise, default parameters can only be defined starting at the end. You cannot define a default parameter fortop
without also declaring one forbottom
.当您执行此操作时:
...
1
的默认值将提供给参数bottom
,因为您的构造函数具有bottom
的默认值范围。 (这就是=1
的意义)如果您要更改构造函数的声明以不包含任何默认值:
...那么您将无法再构造
Rational< /code> 对象而不显式指定两个参数。
When you do this:
...the default value of
1
is supplied to the parameterbottom
, because your constructor has default values for thebottom
parameter. (That's that the=1
is about)If you were to change the declaration of th constructor to not include any defaults:
...then you would no longer be able to construct a
Rational
object without specifying both parameters explicitly.为了扩展 Rob Kennedy 的答案,这里有一些有效和无效的示例:
想象一个 Foo 类:
并考虑以下构造函数调用:
请注意,变量仍然是从左到右传递的。我的意思是,您不能省略您想要显式传递的参数之前的任何参数。
我的意思是,在上面的示例中,您不能传递 b 的值,但可以省略 a 的值。
To extend Rob Kennedy's answer, here are a few examples of what does and does not work:
Imagine a class Foo:
And consider the following constructor-calls:
Mind you that variables are still passed left-to-right. By this I mean that you cannot leave out any argument that comes before the argument that you want to explicitly pass.
By this I mean that in the above example, you can not pass a value for b, but leave out a value for a.
top
和bottom
是具有隐式值的参数。这意味着您可以在调用函数时跳过它们,并且它们的隐式值将用于调用函数。因此,对于给定的构造函数,您可以说
Rational r;
并且 r 将具有顶部 0 和底部 1,或Rational r(42)
,在这种情况下,顶部将为 42 并且底部为 1,或Rational r(1,2);
,顶部为 1,底部为 2。top
andbottom
are arguments with implicit values. That means you can skip them when calling the function and their implicit values will be used for calling the function.So with the given constructor you can say
Rational r;
and r will have top 0 and bottom 1, orRational r(42)
, in witch case top will be 42 and bottom 1, orRational r(1,2);
and top be 1 and bottom 2.