int value = 12;
Here 'value' is a value variable.
where int & is used to create a reference variable (alias) for another variable. how
int i; int& j=i;
j and i refer to the same memory location. it c++ concept.
But a reference variable cannot be created for a memory location. Therefore, in my idea, the following expression is a promt error
int& i = 12; `Will not compile even.`
About const:
The const keyword is used to create a read-only variable. After initialization, the value of the variable cannot be changed. consider the following code:
const int x = 2;
Error: Error: Read-only variable assignment 'x'
Also, the expression can use const on both sides of the type.
const int value = 12; int const value = 12;
Both expressions are the same (for simple data types other than pointers). learn from here The most common places we see are function arguments or return values.
const with reference variable :
A temporary one is created and it is legal to bind a const link to it, but illegal to bind a const to it.
It's simple:
const int& reference_to_const_int = int(20);
A const link extends the life of the temporary, so this works. This is just a language rule. (I learned from here )
source share