Why once I've declared a reference as a const then it can take a different type of data?

c++, oop

Solution

What's happening behind the scenes is an implicit conversion of an int to a double. The result of an implicit conversion is not an lvalue, so it cannot be used to initialize a non-const reference.

Problem

Hello i'm trying to figure out this thing.. Say i have this code. ``` int a = 5; double& b = a; //Error. ``` Then once I've declared the second line as a const the compiler doesn't complain anymore. ``` const double& b = a; //Correct. ``` what is really going on behind the scene, why const solves the problem.

Original source

Related problems