What is a constant reference? (not a reference to a constant)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



What is a constant reference? (not a reference to a constant)



A pretty theoretical question...Why constant references do not behave the same way as constant pointers and I can actually change the object they are pointing to? They really seem like another plain variable declaration. Why would I ever use them? This is a short example that I run which compiles and runs with no errors:


int main ()
int i=0;
int y=1;
int&const icr=i;
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99; // Can assign another value but the value is not assigned to y...
int x=9;
icr=x;
cout<<"icr: "<<icr<<", y:"<<y<<endl;





Look at i before and after icr=y;.
– Ben Voigt
Sep 14 '11 at 17:59


i


icr=y;





Does that even compile?
– Kerrek SB
Sep 14 '11 at 18:07





see parashift.com/c++-faq-lite/const-correctness.html#faq-18.7
– Felix Dombek
Sep 14 '11 at 18:15





check the value of x at the end . its 9 . since icr which refers to i has been changed to 9.
– Kenta
Dec 24 '12 at 11:37





This doesn't compile!! int&const icr=i; is wrong.
– Allanqunzi
Jul 7 '15 at 2:03



int&const icr=i;




5 Answers
5



The clearest answer.
Does “X& const x” make any sense?



No, it is nonsense



To find out what the above declaration means, read it right-to-left:
“x is a const reference to a X”. But that is redundant — references
are always const, in the sense that you can never reseat a reference
to make it refer to a different object. Never. With or without the
const.



In other words, “X& const x” is functionally equivalent to “X& x”.
Since you’re gaining nothing by adding the const after the &, you
shouldn’t add it: it will confuse people — the const will make some
people think that the X is const, as if you had said “const X& x”.



The statement icr=y; does not make the reference refer to y; it assigns the value of y to the variable that icr refers to, i.


icr=y;


y


y


icr


i



References are inherently const, that is you can't change what they refer to. There are 'const references' which are really 'references to const', that is you can't change the value of the object they refer to. They are declared const int& or int const& rather than int& const though.


const


const


const


const int&


int const&


int& const





So a reference to a const can also be seen as a constant reference to a const? That makes a lot of sense now.
– Dasaru
Sep 27 '12 at 23:07





@Dasaru: Yes, it can, but a reference to non-const can also be seen as a constant reference to non-const, as the reference itself is always const, independently from if it references a const or not.
– Kaiserludi
Jan 12 '15 at 13:04



What is a constant reference (not a reference to a constant)

A Constant Reference is actually a Reference to a Constant.



A constant reference/ Reference to a constant is denoted by:


int const &i = j; //or Alternatively
const int &i = j;
i = 1; //Compilation Error



It basically means, you cannot modify the value of type object to which the Reference Refers.
For Example:

Trying to modify value(assign 1) of variable j through const reference, i will results in error:


1


j


i



assignment of read-only reference ‘i’


icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99;



Doesn't change the reference, it assigns the value of the type to which the reference refers.
References cannot be made to refer any other variable than the one they are bound to at Initialization.



First statement assigns the value y to i

Second statement assigns the value 99 to i


y


i


99


i





How & why statement like const int& a=3; valid?
– Destructor
Jul 17 '15 at 17:40





Wait, const Type& is equivalent to Type const&?
– Tyler
Sep 20 '15 at 1:42


const Type&


Type const&



By "constant reference" I am guessing you really mean "reference to constant data". Pointers on the other hand, can be a constant pointer (the pointer itself is constant, not the data it points to), a pointer to constant data, or both.





The sample code actually refers to a constant reference (int & const icr = i;) not a reference to a constant.
– Void
Sep 14 '11 at 18:07



int & const icr = i;





I assumed the poster wasn't clear because of where the const was placed in the code.
– Poodlehat
Sep 14 '11 at 19:22



First I think int&const icr=i; is just int& icr = i, Modifier 'const' makes no sense(It just means you cannot make the reference refer to other variable).


int&const icr=i;


int& icr = i


const int x = 10;
// int& const y = x; // Compiler error here



Second, constant reference just means you cannot change the value of variable through reference.


const int x = 10;
const int& y = x;
//y = 20; // Compiler error here



Third, Constant references can bind right-value. Compiler will create a temp variable to bind the reference.


float x = 10;
const int& y = x;
const int& z = y + 10;
cout << (long long)&x << endl; //print 348791766212
cout << (long long)&y << endl; //print 348791766276
cout << (long long)&z << endl; //print 348791766340






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS