Principle cast

Author: pansz
link: https: //www.zhihu.com/question/20482511/answer/15262336
Source: know almost
copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

The so-called mandatory conversion, in fact, there are different types. A cast statement, in different places, have different meanings. General view, in the C language, the cast has two meanings:

1, a data copy to another data type, and the use of new types of data, this time can be considered a new type of data structure in memory without any relation to the original data, which is the real conversion occurred , and the relevant code to perform this conversion is generated by the compiler. E.g:
char c = 'c';
int i = (int) c;
In the above example, the character is converted into c int, the two are completely different data, length memory footprint which are completely different.
2, a data, as another data type, so the compiler that this is another data type, this time without any data conversion takes place, nothing happens in the computer's point of view, it does not generate any code for this conversion , but the compiler think that this statement is illegal. E.g:
long l = 0x10000000L;
volatile void *p = (void *) l;

In this sentence, the variable l is "simply as" pointer p, any conversion did not happen in terms of memory data, this conversion just let the compiler think the only legitimate assignment.
In C ++, which is generally referred to reinterpret_cast second type, while the first type of directly using explicit type conversions achieved.

In Java, will usually depending on the context, two different situations 1 and 2 occurs.

Guess you like

Origin blog.csdn.net/alss1923/article/details/78889971