-
December 24th, 2012, 08:34 AM
#1
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 ?
-
December 24th, 2012, 10:44 AM
#2
Re: Reference with constant modifier
In real time programming we don't implement any concepts, just make things done... Mostly in C.
-
December 24th, 2012, 11:00 AM
#3
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 ?
-
December 24th, 2012, 11:54 AM
#4
Re: Reference with constant modifier
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
-
December 26th, 2012, 12:00 PM
#5
Re: Reference with constant modifier
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.
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|