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

• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 商业

BUSINESS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本 扫码下载译学馆APP

#### 《卷轴教程之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

in the Star Wars series.

So while he was in that movie,

he was playing a role as R2-D2, but outside of that role

he was, he was Kenneth Baker.

And we have a very similar concept that we can apply in programming.

So say we have the following C++ code.

So we have a declaration of a variable
double类型的变量
of type double called “x”

and we’re assigning the value 5.25

And then we have below that the declaration of a variable

of type int, and has the name “y”.

And if we do this sort of assignment operation

where we assign the value of x to y in C++.

What are we doing? We are actually taking a value that

subtype double which may be represented by 8 bytes

And storing in to a variable that only has space

allocated for 4 bytes.

And also what do we know about ints?

They are not able to represent a fractional part.

So what happens is C++, it actually allows this operation to occur.

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

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

it would like to have the value x,

at least for this operation here,

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,

where we have maybe y

and we are dividing it by 10.

So if look at what we have so far here

the value of y should be 5, right?

Originally, we had x assigned 5.25 ,

then we assign that value to y, y was of type int,

So we lost the fractional part in both of these cases here.

And so it should just be an integer represantation of 5 here.

So we have 5 divided by 10.

So the result to that should be one half – 0.5.

But x over here is of type double

this is of type int and this is what we call an int literal.

So whenever we perform this operation here,

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.

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.

So we can actually explicitly cast, either the y or the 10,

to be of type double and then we’ll show

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

So you can do the same thing here in front of the 10.

We are changing this to be of type double.

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.

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.

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.

So any time we use y after that

it would still be represented of an int.

It was only playing the role of a 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