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.
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.
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.
constdouble& 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:
1 2
double val = 66.6;
// As simple as this, val can be used as a variable reference
Defining it as constdouble &val = //numerical value will get you the same result as declaring it as constdouble val = //numerical value , and the difference between the constdouble 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.
// Libraries definition
// Namespace definition
constshort ID_length = 10; // The length of a credit card serial number is 10.
constshort 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.
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.
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.
@EssGeEich :Thanks for your reply .Please see the edited post .
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.