Skip to main content
added 56 characters in body
Source Link
Slava
  • 44.4k
  • 2
  • 54
  • 100

To understand it better you can look into difference between const pointer and pointer to a const data:

int i, j; const int *p1 = &i; // pointer to constant int int *const p2 = &i; // constant pointer to int *p1 = 0; // error, p1 points to const int *p2 = 0; // it is fine sets i to 0 p1 = &j; // fine p1 now points to anbother int p2 = &j; // error, p2 is a constant pointer 

so now if we replace pointer to reference we can see similar things, except reference by itself is not changeable ie you cannot make reference to refer to another object after it is created (unlike non constant pointer) and any reference is like constant pointer. So const reference in this meaning does not make any sense and usually by const reference people mean reference to a const type. That what quote from primer means.

As for difference in your code, yes there is difference - you cannot change object through const reference does not matter if that reference points to const object or not.

To understand it better you can look into difference between const pointer and pointer to a const data:

int i, j; const int *p1 = &i; // pointer to constant int int *const p2 = &i; // constant pointer to int *p1 = 0; // error, p1 points to const int *p2 = 0; // it is fine sets i to 0 p1 = &j; // fine p1 now points to anbother int p2 = &j; // error, p2 is a constant pointer 

so now if we replace pointer to reference we can see similar things, except reference by itself is not changeable ie you cannot make reference to refer to another object after it is created (unlike pointer). So const reference in this meaning does not make any sense and usually by const reference people mean reference to a const type. That what quote from primer means.

As for difference in your code, yes there is difference - you cannot change object through const reference does not matter if that reference points to const object or not.

To understand it better you can look into difference between const pointer and pointer to a const data:

int i, j; const int *p1 = &i; // pointer to constant int int *const p2 = &i; // constant pointer to int *p1 = 0; // error, p1 points to const int *p2 = 0; // it is fine sets i to 0 p1 = &j; // fine p1 now points to anbother int p2 = &j; // error, p2 is a constant pointer 

so now if we replace pointer to reference we can see similar things, except reference by itself is not changeable ie you cannot make reference to refer to another object after it is created (unlike non constant pointer) and any reference is like constant pointer. So const reference in this meaning does not make any sense and usually by const reference people mean reference to a const type. That what quote from primer means.

As for difference in your code, yes there is difference - you cannot change object through const reference does not matter if that reference points to const object or not.

Source Link
Slava
  • 44.4k
  • 2
  • 54
  • 100

To understand it better you can look into difference between const pointer and pointer to a const data:

int i, j; const int *p1 = &i; // pointer to constant int int *const p2 = &i; // constant pointer to int *p1 = 0; // error, p1 points to const int *p2 = 0; // it is fine sets i to 0 p1 = &j; // fine p1 now points to anbother int p2 = &j; // error, p2 is a constant pointer 

so now if we replace pointer to reference we can see similar things, except reference by itself is not changeable ie you cannot make reference to refer to another object after it is created (unlike pointer). So const reference in this meaning does not make any sense and usually by const reference people mean reference to a const type. That what quote from primer means.

As for difference in your code, yes there is difference - you cannot change object through const reference does not matter if that reference points to const object or not.