Is there any difference between line 2 and line 3 in the following code? What 开发者_开发知识库does compiler do in each case?
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++)?
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&).
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.
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.
That said, the best is to avoid casts to the degree possible.
Cheers & hth.,
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.
End result is the same (that is both your int are valued at 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.
if this code is part of a C++ app, it would be even better to use static_cast for at least 2 reasons:
- 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
- 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
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.
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:
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...
精彩评论