ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#7 类型转换 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#7 类型转换

Casting in C++

好的 现在我们来谈谈类型转换以及它的实际意义
All right. So let’s talk a little bit about casting and what it actually means.
让我们先想一下在电影中一个演员扮演某个特定角色
So we can think about an actor being cast in a specific role in a movie.
这张图片中的人就是星球大战系列中
So over here in this picture we have Kenneth Baker, who’s the actor that played R2-D2
扮演R2-D2的演员Kenneth Baker
in the Star Wars series.
在这部电影中
So while he was in that movie,
他扮演R2-D2这个角色 但是在戏外
he was playing a role as R2-D2, but outside of that role
他是Kenneth Baker
he was, he was Kenneth Baker.
那么 对于编程 我们可以有相似的概念
And we have a very similar concept that we can apply in programming.
在接下来的C++代码中
So say we have the following C++ code.
我们声明一个名为x的
So we have a declaration of a variable
double类型的变量
of type double called “x”
并且给这个变量赋值为5.25
and we’re assigning the value 5.25
然后在它(x)下面再声明一个int类型的变量
And then we have below that the declaration of a variable
变量名为y
of type int, and has the name “y”.
如果我们以上图的方式进行赋值运算
And if we do this sort of assignment operation
在C++中x的值会被赋给y
where we assign the value of x to y in C++.
我们做了什么呢 实际上我们正在将
What are we doing? We are actually taking a value that
占用8个字节的double类型数值
subtype double which may be represented by 8 bytes
存储到仅占4个字节空间
And storing in to a variable that only has space
的变量中
allocated for 4 bytes.
同样的 我们对int类型了解多少呢
And also what do we know about ints?
它们(int)不能用来表示小数部分
They are not able to represent a fractional part.
那么在C++中发生了什么呢 实际上C++允许这个运算执行
So what happens is C++, it actually allows this operation to occur.
只不过取决于你拥有何种C++编译器
Depending on what kind of compiler you have,
你有可能得到警告 也可能没有
C++ compiler you have. You may get a warning or you may not.
不管你会不会得到警告
Regardless if you get the warning or not,
其实这都暗示着运算可以执行
implicit and implied cast is occuring here.
所以那就意味着 编译器
So what that means is the compiler is taking care of
会帮你把double类型数值转化成int类型数值了
transforming or converting the double value into an int value for you.
现在你还可以做的是被称为”显式转型”的操作
Now you could also do what’s called an ‘explicit cast’.
该方法实际就是 告诉编译器
What this means is you’re actually telling the compiler
把这个特定值通过这个具体表达式转换成其他的某种结果
I want to convert this particular value for this specific expression to something else.
所以 我们可以做一件非常相似的事
So we could do a very similar thing here
就是这个作明确申明的部分
where we’ve actually explicitly stated
它将把这个数值x
it would like to have the value x,
至少是在这里的
at least for this operation here,
赋值运算中 把它显式转换成一个int类型
this assigned operation, to be an int value.
这个显式转型所做的事情和之前的直接赋值是一样的
Now the same thing happen behind the scenes here
只不过在这里我做了明确声明
but here we’re being very explicit about it.
所以如果你有一个编译器
So if you have a compliler that
可能会因为损失精度
may have complained here provided some kind of warning
而给出警告
saying that we may have lost some precision.
在这个例子中 你已经告诉编译器
In this particular case, you are letting the compiler know that you are
你已经准备好丢失小数部分
ok with that lost of precision that you don’t
因为你根本不关心小数部分
not concerned about the fractional part.
那么接下来我们继续来看这里的另一个例子
So let’s look at another example real quick here,
假设我们这里有一个y
where we have maybe y
然后让它除以10
and we are dividing it by 10.
那么到目前为止 我们所得到的y呢
So if look at what we have so far here
它的数值应该是5 对吗
the value of y should be 5, right?
起初 我们给x赋值为5.25
Originally, we had x assigned 5.25 ,
然后再把x的值赋给y y是int类型的变量
then we assign that value to y, y was of type int,
在这两个案例中我们都丢失了小数部分
So we lost the fractional part in both of these cases here.
所以这里它应该仅仅表示整型的5
And so it should just be an integer represantation of 5 here.
然后再5除以10
So we have 5 divided by 10.
那么这个结果就是1的一半 即0.5
So the result to that should be one half – 0.5.
但是这里的x是double类型的
But x over here is of type double
而这个是int类型 而这个我们称之为整型参数
this is of type int and this is what we call an int literal.
那么无论何时 执行这里的运算
So whenever we perform this operation here,
由于我们有一个int类型数值 然后除以一个int类型数值
since we have an int, divided by an int,
最后 刚才所执行的运算 我们称之为整型除法
What happens here is we end up with what we called integer division
实际上我们就丢失了小数部分
and we actually lose the fractional part.
所以这是不够好的 尽管我们把值赋给double类型的x
So this is not good enough. So even though we are asigning it to x, which is of type double,
但在赋值运算前就已经丢失小数部分了
we are losing the fractional part before we even get to the assignment operation.
其实这个结果糟透了 在这个案例中为了避免结果不精确
But this is not a good thing. We actually need to do an explicit cast in this case
实际上我们需要一个显示转换
to avoid losing that precision.
那么我们可以做显式转换 转换y或者说10
So we can actually explicitly cast, either the y or the 10,
把它们变成double类型的数值 我们这
to be of type double and then we’ll show
所要写的是如何对y进行显式转换
how to just do an explicit cast for the y value.
所以实际上这就是我们一直在提的显示转换
So here we can actually do this explicit cast.
那么我们所做的就是在变量的前面
So what we’ve done is just put the name or the type that we wanna have
放置名称或者说是放置我们想要的类型
in front of the variable of the value
所以你可以在这个10之前做同样的事
So you can do the same thing here in front of the 10.
我们正把它改变成double类型
We are changing this to be of type double.
所以 现在我们有了一个double类型的 这里有一个int类型的
So now we have a double here and we have an int here,
最后这个运算 我们称之为浮点除法的运算
so we ends up doing what we called a floating point division.
所以实际上它促使这个家伙变成double类型
So it actually is promoting this guy to be of type double
执行浮点除法运算
and it performs a floating point division.
所以在那个案例中 实际上我们并没有降低掉精确度
So in that case we actually do not lose any precision there.
现在 如果我们 实际上 讨论变量y
Now, if we were to come down below here and actually talk about the variable y,
y将仍然是一个int数值
y would still be an int value.
y仅仅作为double类型计算 被用于这里的特别运算 就是这个赋值运算
It was only cast as a double for this particular operation here, this assignment operation.
所以在那之后无论何时我们使用y
So any time we use y after that
它仍然表示一个int类型数值
it would still be represented of an int.
它仅仅扮演着double的角色
It was only playing the role of a double.
或者说 在这个运算中 它饰演了double这个角色
Our cast is a double in this specific statement.

发表评论

译制信息
视频概述

double?不 int?不 double or int?

听录译者

金鱼汤

翻译译者

[B]Azrael

审核员

豆子

视频来源

https://www.youtube.com/watch?v=oa_fWKQACAo

相关推荐