(1) first, let's look at the differences between (INT) and (Int &) through an example:
Float a = 1.0f;
Cout <(INT) A <Endl;
Cout <(Int &) A <Endl;
Cout <boolalpha <(INT) A = (Int &) a) <Endl; // boolalpha represents true and false in symbolic form
Float B = 0.0f;
Cout <(INT) B <Endl;
Cout <(Int &) B <Endl;
Cout <boolalpha <(INT) B = (Int &) B) <Endl;
Output:
1
1065353216
False
0
0
True
Explanation:
(INT) A actually constructs an integer with the floating point A as the parameter. The value of this integer is 1;
(Int &) A tells the compiler to treat a as an integer (there is no substantial conversion)
Because 1 is stored as an integer and its memory data is stored as a floating point, the two are not the same.
However, the integer form of 0 is the same as that of floating point, so in this special case, the two are equal (only in the numerical sense ).
Note:
The output of the program will show (Int &) A = 1065353216. How does this value come from? As mentioned above, 1 is stored in the memory as a floating point number. According to ieee754, the content of 1 is 0x0000803f (Bytes reverse order has been considered ). This is the value of the memory unit occupied by the variable. When (Int &) A appears, it is equivalent to telling its context: "treat the content in this address as an integer! Don't worry about what it was ." In this way, the content 0x0000803f is interpreted as an integer, and its value is exactly 1065353216 (decimal number ).
By viewing the assembly code, we can confirm that "(INT) A is equivalent to re-constructing an integer number with a value equal to a", while (Int &) the function is only to express a type information, meaning that the correct Heavy Load version is selected for cout <and =.
(2) (int *) is to display a variable (this variable cannot be a floating point type, it can be an integer, character, pointer) in the form of an address, and (Int &) the principle is the same. When (Int & *) A appears, it is equivalent to telling its context: "treat the content in this address as an address! Don't worry about what it was ."
For example:
Char P = 'a ';
Cout <(INT) P <Endl;
Cout <(int *) P <Endl;
Output:
97
00000061 // The hexadecimal number of 97
This is useful when outputting the first address of the character array:
Char * P = "ABCD ";
Cout <(int *) P <Endl;
Otherwise, cout <p <Endl;
The output is ABCD rather than the first address.