Reference with constant modifier
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 ?
Re: Reference with constant modifier
In real time programming we don't implement any concepts, just make things done... Mostly in C. :)
Re: Reference with constant modifier
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 ?
Re: Reference with constant modifier
Quote:
Originally Posted by
vikuseth
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.
Regards,
Paul McKenzie
Re: Reference with constant modifier
Quote:
Originally Posted by
vikuseth
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.