define the variable float a, convert a to float and and int &, what does this mean? After conversion, a is a reference to yourself? And why are the two results different?
#include <iostream> using namespace std; int main(void) { float a = 1.0; cout << (float &)a <<endl; cout << (int &)a << endl; return 0; } thinkpad ~ # ./a.out 1 1065353216
cout << (float &)a <<endl; cout << (int &)a << endl;
The first handles the bits as a float. The second considers bits in like an int. The bits for float 1.0 are simply bits for the integer 1065353216.
This is basically the equivalent of:
float a = 1.0; int* b = (int*) &a; cout << a << endl; cout << *b << endl;
(int &) apasses a reference to an integer. In other words, the whole link to a. (Which, as I said, treats the contents of a as a whole.)
(int &) a
Edit: Now I look back to find out if this is true. I suspect this is not the case. It depends on which type is smaller or equal to the actual size.
undefined: -).
, . a float, a ( ) . (float&)a , float ( , ); (int&)a int. (, a) lvalue , , - undefined, . , , , .
a
float
(float&)a
(int&)a
int
float , . , , 0.0 0 . ( , , , 2 1. 1.0 0.) ( ), . , "" " " , , .
0.0
1.0
, a float int & ( int) . a int, , . , float, int, 1065353216, 42 .
int &
1065353216
42
, , int, , float int. int & , , .