C and C ++: the difference between casting and conversion

Is there a difference between line 2 and line 3 in the following code? What does the 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 ++)?

+7
c ++ c casting
source share
4 answers

There is no difference in the final effect.

A cast - Use explicit, common inline 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 a new musical notation is defined.

The above definition of terminology is just an operational definition, that is, it is not a definition in which you can reason that something is an act. Throws are those that are defined as drops. :-) For example, bool(x) is cast, and !!x , which does the same and is also an explicit designation, is not cast.

In C ++, you can and preferably use the named roles static_cast , const_cast , dynamic_cast and reinterpret_cast , with a possible exception, for casting arithmetic built-in types explicitly. One reason is that the C style created by (Other*)p , or in C ++, the specific Notation OtherPtr( p ) , can do different things depending on the context, and in particular when the code changes slightly, C style value can be changed. Another reason is that it’s hard to look for C. styles.

However, it is best to avoid throwing as much as possible.

Cheers and hth.,

+4
source share

Both of them are transformations / translations, in line 2 they are simply implicit, and in line 3 they are explicit, there is no functional difference.

+2
source share

The end result is the same (that is, both int values ​​are evaluated at 65).

now line 3 allows the reader (or someone may have to maintain the code) to determine listing C; which, in my humble opinion, is a plus.

if this code is part of a C ++ application, it would be better to use static_cast for at least two reasons:

  • it’s much easier to find static_cast in your application that has a C style; in addition to a clearer understanding of your intent to read the code to someone else.
  • C ++ syntax syntax is lengthy, which helps to limit casting several times (casting is still sometimes necessary :). If you go from character to things to convert strings and numbers, you will have to use something like streams for example, anyway

    hope this helps.

0
source share

Conversion is the process of converting data from one type to another. Listing is the operator that invokes the conversion (unless the types already match).

In C, most throws are unnecessary and are considered bad. In C ++, a C-style cast is considered by many to be a bad style; C ++ has a safer casting system, but since I do not use C ++, I will 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 includes a conversion to a smaller type (from int to char ), while the second and third lines include a conversion to larger types. It's pretty stupid to do an explicit (never dangerous) conversion to a larger type if you omit (in some cases dangerous, but not here) conversion to a smaller type on line 1. Of course, that would be even dumber:

 char ch = (char)'A'; 

In most cases, if you find that you need to quit, it means that you are doing something wrong, or something pretty smart ...

0
source share

All Articles