brian14708
brian14708

Reputation: 1961

C++ type conversion from a variable to a reference

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

float f = 1.0f;
int a = (int&) f;
std::cout << a;

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

Upvotes: 0

Views: 514

Answers (3)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

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?

Upvotes: 4

Alex Shtoff
Alex Shtoff

Reputation: 2640

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.

Upvotes: 1

Platinum Azure
Platinum Azure

Reputation: 46183

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

Upvotes: 0

Related Questions