Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query related with the constant storage

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi
I have one basic query regarding how the system stores the Constants.

If i have the following code

Code:
double &dr = 1 ; // error: lvalue needed
constdouble &cdr = 1 ;

As the comment suggest, the lvalue required fot the plain reference but not for the const double&. Is there any specific reason for this in the context of storage of normal variable v/s constants ? I am little confussed on this.

Thanks in advance.

sanjay
 
You want to initialize a reference to the (constant!) 1 (one). Well, you may get a constant reference to a constant (the language creates implicit variable with value 1, line #2), but you can't create a normal reference with read/write access rights (line #1). It's (reasonable) C++ rules.

If it's possible, a compiler places all constants in read-only pages/segments, but it's another story. Probably, you want symbolic named constant (double one = 1), not a reference to constant?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top