double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
can anyone please let me know where exactly we can use this concept in real time programming ?
int nVar = 12;
int &rVar = nVar ;//Ok
double &dVar = nVar ;//Error
const double &cdVar = nVar ;//Ok
can you please explain Why the 3rd statement is not working where as 4th statement is working ?
What is "dVar"? What is "cdVar"?
Please post real code that can be compiled, and please use code tags. If you're asking a question why something doesn't compile, it is imperative you post the actual, real bonafide C++ code that doesn't compile, and not shortcuts or psuedo-code.
Edit: Look at my post and look at yours. Do you see why you need code tags? No one knows whether that & is a reference, or the fault of your posting.
double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
A const reference can bind to an r-value, a (non-const) reference cannot. The literal is an r-value. That's all the magic.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks