### 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 .
The 'const', used with a reference, allows the copy of a value.
The first line is wrong because 66.6 is a constant, it cannot be a variable reference.
The second line is right because 66.6 is a constant and it can be a constant reference.

Kinda simple, huh?
You could definitely declare a const with an already defined value. However, defining the dereferenced variable as a const is not allowed.
To explain this, the dereference operator & used on a variable, works only on pointer variables. In your case, the number is a const given number, so you cannot dereference it.
For further explanations, check tutorials for pointers.

Hope I could help,
~Raul~
Last edited on
kbw wrote:

I don't know if you said "examples please" at my post, so...

`double& val = 66.6;`
Besides from this being incorrect, val is a "variable-reference" (Reference to a variable). 66.6 is CONSTANT, is not VARIABLE, so it's wrong, it's an error.

`const double& val = 66.6;`
val is a "constant-reference" (Reference to a constant). 66.6 is CONSTANT so it's right.

If you wanted to copy 66.6 into val, with val being a variable-reference, you can simply:
 ``12`` ``````double val = 66.6; // As simple as this, val can be used as a variable reference ``````

WARNING: Things over have been simplified.
Last edited on
what is the need of doing
const double &val = 66.6 as we can simply do double val = 66.6 ;
Is there any importance of going for the above concept ?
Defining it as `const double &val = //numerical value ` will get you the same result as declaring it as `const double val = //numerical value `, and the difference between the `const double val = //numerical value ` and `double val = //numerical value `, is that you will never be able to modify the const one, so preventing it from being overwritten.

Example: You want to read a number with a constant number of digits. For example a credit card number.

 ``123456789101112131415161718192021`` ``````// Libraries definition // Namespace definition const short ID_length = 10; // The length of a credit card serial number is 10. const short MaxEntries = 100; // The number of maximum entries is 100. short serialNumberCreditCard[MaxEntries][ID_length]; // You will read at most 100 credit cards entries with the length of 10 int main(int argc, char *argv[]){ short nrOfEntries = 0; cin >> nrOfEntries; for (short i = 1; i <= nrOfEntries; i++){ for (short j = 1; j <= ID_length; j++){ cin >> serialNumberCreditCard[i][j]; // Reads one digit at a time; } if (j < ID_length){ // If the length is not 10, then the entry is invalid, so we will reset it and read it again for (short k = 1; k <= j; k++){ serialNumberCreditCard[i][k] = 0; } i--; } } // rest of code }``````

You could as well declare the ID_length and MaxEntries as normal variables, but that would allow the programmer to change their values during the coding, whereas declaring them as const, will not allow the program to change those values, returning an error.

Hope I could help,
~Raul~
Last edited on
> Is there any importance of going for the above concept ?
You could extend it to T where the copy may be costly.
 ``123`` ``````void foo(T&); foo( give_me_a_temporary() ); //will not compile ``````
@ne55 : can you please explain something more on your post ?
Last edited on
@EssGeEich::
As you said in your post , since 66.6 is a constant (not a variable ) , so we have to use const modifier while using reference .

int nVar = 12;
int &rVar = nVar ;//Ok
double &dVar = nVar ;//Error
const double &cdVar = nVar ;//Ok

Why the 3rd statement is not working where as 4th statement is working ?
Last edited on
@vikuseth
 But in the below case num is a variable then why should we use const int num = 10; int & post = num;//Error const int & post = num;//Ok

Please check a tutorial on pointers and data types.
If you still didn't understand my explanations, you definitely need to start studying it by yourself.

Reference:
http://www.cplusplus.com/doc/tutorial/variables/
http://www.cplusplus.com/doc/tutorial/constants/
http://www.cplusplus.com/doc/tutorial/pointers/

Come back after reading that thoroughly.

~Raul~
Last edited on
`int & post = num;//Error `
Not an error. Num is a variable. You should use const if you don't edit it and want to use the conventions everyone else uses.
Last edited on
i am new to c++ .so sorry i am not getting the concept in a single go and asking you all a lot of stupid questions .
 As you said in your post , since 66.6 is a constant (not a variable ) , so we have to use const modifier while using reference . int nVar = 12; int &rVar = nVar ;//Ok double &dVar = nVar ;//Error const double &cdVar = nVar ;//Ok Why the 3rd statement is not working where as 4th statement is working ?

The third line won't work because nVar is not of type double, and you can't alias an int with a reference to double.

The fourth line creates a temporary from nVar of type double and binds the reference to that temporary. The reason that doesn't happen for the third line is because temporaries will not bind to a non-const reference.

 ``1234567891011121314151617`` ``````#include int main() { int nVar = 12; int &rVar = nVar ;//Ok // double &dVar = nVar ;//Error const double &cdVar = nVar ;//Ok std::cout << "Address of nVar: " << &nVar << "\nAddress of rVar: " << &rVar << "\nAddresss of cdVar: " << &cdVar << '\n' ; nVar = 13 ; std::cout << nVar << ' ' << rVar << ' ' << cdVar << '\n' ; }`````` ```Address of nVar: 0331F770 Address of rVar: 0331F770 Addresss of cdVar: 0331F748 13 13 12```

Notice that cdVar is not a reference to nVar.
Last edited on
Topic archived. No new replies allowed.