Is there any difference between line 2 and line 3 in the following code? What does compiler do in each case?
以下代码中的第2行和第3行有什么区别吗?编译器在每种情况下做什么?
char ch = 'A'; //line 1
int i = ch; //line 2
int j = (int) ch; //iine 3
In general, what is the difference between Casting and Conversion (in C and C++)?
一般来说,铸造和转换(在C和c++中)的区别是什么?
4 个解决方案
#1
4
There's no difference in the final effect.
最终的效果没有区别。
A cast is to use explicit, general, built-in cast notation for conversion.
强制转换是使用显式的、通用的、内置的强制转换表示法进行转换。
Although in some cases we say "up-cast" when we mean an implicit conversion from Derived* to Base* (or from Derived& to Base&).
虽然在某些情况下,当我们指从派生*到Base*(或从派生&到Base&)的隐式转换时,我们会说“向上转换”。
And in some cases one defines new cast notation.
在某些情况下,一个定义了新的铸造表示法。
The above definition of the terminology is just an operational definition, that is, it's not a definition where you can reason out that something is a cast. Casts are just those that are defined as casts. :-) For example, bool(x)
is a cast, while !!x
, which does the same and also is explicit notation, is not a cast.
上述术语的定义只是一个操作定义,也就是说,它不是一个定义,你可以推断出某个东西是一个类型。强制类型转换只是定义为强制类型转换的那些类型。例如,bool(x)是一个角色,而!x也是显式符号,它不是一个类型转换。
In C++ you can and preferably should use the named casts static_cast
, const_cast
, dynamic_cast
and reinterpret_cast
, with possible exception for explicit casting of arithmetic built-in types. One reason is that a C style cast (Other*)p
, or in C++-specific notation OtherPtr( p )
, can do different things depending on context, and in particular, when the code is slightly changed the meaning of a C style cast can change. Another reason is that it's difficult to search for C style casts.
在c++中,您可以并且最好应该使用已命名的cast static_cast、const_cast、dynamic_cast和reinterpretation t_cast,但是对于算术内置类型的显式casting可能有例外。一个原因是,C风格强制转换(Other*)p,或者在c++特定的符号OtherPtr(p)中,可以根据上下文做不同的事情,特别是当代码稍微改变了C风格强制转换的含义时。另一个原因是很难搜索C风格的类型转换。
That said, the best is to avoid casts to the degree possible.
也就是说,最好的方法是避免被强制转换到可能的程度。
Cheers & hth.,
欢呼声& hth。
#2
2
Both of them are conversions/casts, in line 2 it's just implicit, while on line 3 it's explicit, there's no functional difference.
它们都是转换/强制转换,在第2行它是隐式的,而在第3行它是显式的,没有函数差异。
#3
0
End result is the same (that is both your int are valued at 65).
最终结果是相同的(即您的int值为65)。
now, line 3 allows the reader (or whomever might have to maintain the code) - to spot the C cast; which is in my humble opinion a plus.
现在,第3行允许读者(或者可能需要维护代码的人)找到C类型转换;在我看来,这是一个优点。
if this code is part of a C++ app, it would be even better to use static_cast for at least 2 reasons:
如果这段代码是c++应用程序的一部分,那么使用static_cast至少有两个原因:
- it is much easier to find static_cast in your application that a C style one; in addition to be clearer to understand your intention to someone else reading the code
- 在应用程序中更容易找到static_cast,它是C样式的;此外,要更清楚地了解您的意图,以便其他人阅读代码
- the C++ cast syntax is lengthy, which helps limiting casting some times (casting is still sometimes needed of course :). If you expand from character to things, to do conversion between string and numbers you will have to use something like streams per example anyway
hope it helps
希望它能帮助
#4
0
A conversion is the process of converting data of one type to another. A cast is an operator which causes a conversion (except in the case where types already match).
转换是将一种类型的数据转换为另一种类型的过程。强制转换是一种导致转换的操作符(在类型已经匹配的情况下除外)。
In C, most casts are unnecessary and considered bad style. In C++, C-style casts are considered by many to be bad style; C++ has a safer cast system, but as I don't use C++ I'll leave it to others to explain.
在C语言中,大多数类型转换都是不必要的,并且被认为是糟糕的样式。在c++中,C风格的类型转换被许多人认为是不好的风格;c++有一个更安全的cast系统,但是由于我不使用c++,我将把它留给其他人来解释。
By the way, in your example:
顺便说一下,在你的例子中
char ch = 'A'; //line 1
int i = ch; //line 2
int j = (int) ch; //iine 3
Assuming this is C, your first line involves a conversion to a smaller type (from int
to char
), whereas the second and third lines involve conversions to larger types. It's rather silly to make explicit the (never-dangerous) conversion to a larger type when you're omitting the (in some cases dangerous, but not here) conversion to a smaller type in line 1. Of course this would be even sillier:
假设这是C,第一行涉及到对较小类型(从int到char)的转换,而第二行和第三行涉及到对较大类型的转换。如果在第1行中忽略了(在某些情况下是危险的,但不是在这里)转换为较小类型的转换,那么显式地转换为较大类型(从不危险)是相当愚蠢的。当然,这更愚蠢:
char ch = (char)'A';
Most of the time if you find yourself needing a cast it means you're doing something wrong, or else something pretty clever...
大多数时候,如果你发现自己需要演员阵容,那就意味着你做错了什么,或者你做了一些非常聪明的事情……
#1
4
There's no difference in the final effect.
最终的效果没有区别。
A cast is to use explicit, general, built-in cast notation for conversion.
强制转换是使用显式的、通用的、内置的强制转换表示法进行转换。
Although in some cases we say "up-cast" when we mean an implicit conversion from Derived* to Base* (or from Derived& to Base&).
虽然在某些情况下,当我们指从派生*到Base*(或从派生&到Base&)的隐式转换时,我们会说“向上转换”。
And in some cases one defines new cast notation.
在某些情况下,一个定义了新的铸造表示法。
The above definition of the terminology is just an operational definition, that is, it's not a definition where you can reason out that something is a cast. Casts are just those that are defined as casts. :-) For example, bool(x)
is a cast, while !!x
, which does the same and also is explicit notation, is not a cast.
上述术语的定义只是一个操作定义,也就是说,它不是一个定义,你可以推断出某个东西是一个类型。强制类型转换只是定义为强制类型转换的那些类型。例如,bool(x)是一个角色,而!x也是显式符号,它不是一个类型转换。
In C++ you can and preferably should use the named casts static_cast
, const_cast
, dynamic_cast
and reinterpret_cast
, with possible exception for explicit casting of arithmetic built-in types. One reason is that a C style cast (Other*)p
, or in C++-specific notation OtherPtr( p )
, can do different things depending on context, and in particular, when the code is slightly changed the meaning of a C style cast can change. Another reason is that it's difficult to search for C style casts.
在c++中,您可以并且最好应该使用已命名的cast static_cast、const_cast、dynamic_cast和reinterpretation t_cast,但是对于算术内置类型的显式casting可能有例外。一个原因是,C风格强制转换(Other*)p,或者在c++特定的符号OtherPtr(p)中,可以根据上下文做不同的事情,特别是当代码稍微改变了C风格强制转换的含义时。另一个原因是很难搜索C风格的类型转换。
That said, the best is to avoid casts to the degree possible.
也就是说,最好的方法是避免被强制转换到可能的程度。
Cheers & hth.,
欢呼声& hth。
#2
2
Both of them are conversions/casts, in line 2 it's just implicit, while on line 3 it's explicit, there's no functional difference.
它们都是转换/强制转换,在第2行它是隐式的,而在第3行它是显式的,没有函数差异。
#3
0
End result is the same (that is both your int are valued at 65).
最终结果是相同的(即您的int值为65)。
now, line 3 allows the reader (or whomever might have to maintain the code) - to spot the C cast; which is in my humble opinion a plus.
现在,第3行允许读者(或者可能需要维护代码的人)找到C类型转换;在我看来,这是一个优点。
if this code is part of a C++ app, it would be even better to use static_cast for at least 2 reasons:
如果这段代码是c++应用程序的一部分,那么使用static_cast至少有两个原因:
- it is much easier to find static_cast in your application that a C style one; in addition to be clearer to understand your intention to someone else reading the code
- 在应用程序中更容易找到static_cast,它是C样式的;此外,要更清楚地了解您的意图,以便其他人阅读代码
- the C++ cast syntax is lengthy, which helps limiting casting some times (casting is still sometimes needed of course :). If you expand from character to things, to do conversion between string and numbers you will have to use something like streams per example anyway
hope it helps
希望它能帮助
#4
0
A conversion is the process of converting data of one type to another. A cast is an operator which causes a conversion (except in the case where types already match).
转换是将一种类型的数据转换为另一种类型的过程。强制转换是一种导致转换的操作符(在类型已经匹配的情况下除外)。
In C, most casts are unnecessary and considered bad style. In C++, C-style casts are considered by many to be bad style; C++ has a safer cast system, but as I don't use C++ I'll leave it to others to explain.
在C语言中,大多数类型转换都是不必要的,并且被认为是糟糕的样式。在c++中,C风格的类型转换被许多人认为是不好的风格;c++有一个更安全的cast系统,但是由于我不使用c++,我将把它留给其他人来解释。
By the way, in your example:
顺便说一下,在你的例子中
char ch = 'A'; //line 1
int i = ch; //line 2
int j = (int) ch; //iine 3
Assuming this is C, your first line involves a conversion to a smaller type (from int
to char
), whereas the second and third lines involve conversions to larger types. It's rather silly to make explicit the (never-dangerous) conversion to a larger type when you're omitting the (in some cases dangerous, but not here) conversion to a smaller type in line 1. Of course this would be even sillier:
假设这是C,第一行涉及到对较小类型(从int到char)的转换,而第二行和第三行涉及到对较大类型的转换。如果在第1行中忽略了(在某些情况下是危险的,但不是在这里)转换为较小类型的转换,那么显式地转换为较大类型(从不危险)是相当愚蠢的。当然,这更愚蠢:
char ch = (char)'A';
Most of the time if you find yourself needing a cast it means you're doing something wrong, or else something pretty clever...
大多数时候,如果你发现自己需要演员阵容,那就意味着你做错了什么,或者你做了一些非常聪明的事情……