开发者

C++ type conversion from a variable to a reference

When doing a type conversion from a variable to a reference, like:

float f = 1.0开发者_如何学Cf;
int a = (int&) f;
std::cout << a;

Why do I get the actual bit-representation of the float value (and not the numeric value).


Your code is undefined behavior, technically speaking. Your C-style cast does a reinterpret_cast which isn't defined for convering a float lvalue to int&. Maybe you just wanted to cast to an int?


Because reference in C++ means "another name for". So when you cast something to a reference you just give it another name, and not perform a value conversion from floats to integers.


Why are you converting a variable to a reference and then storing it in a variable?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜