In the C++ primer, I found that const int &
can bind with a int object.I don\'t understand that,because I think const int &
should bind with a
The value of the variable a
which is 0
initially is holded at memory.
If you write later: a=5
, it'll set that value in memory to 5
.
When you write a reference const int & r = a;
you are only saying that r
should access that same place in memory where 5
is being hold.
Because a
is not const
it can modify the value.
This is basically how it works.
There are two things: Const and reference.
Reference is just another name for same memory unit. You can use any name to change the value held at that memory.
A const reference is denoted in C++ by a code like
int j =2;
int const &i = j; //or Alternatively
const int &i = j;
There is no restriction which makes the referred object to be constant as well.
Here, you can not use i to change value of j. However this does not make the memory location j to be constant.You can peacefully change the value of j.
j =3
//but i = 3 will raise an error saying
//assignment of read-only reference ‘i’
int a=0; const int &r=a; a=3; cout<<r<<endl;
The output is that r=3; the value of r can be changed, why? I don't understand that.
The const
only applies to the reference r
. It means that you can't change whatever r
binded to via r
. You can't do:
r = 3;
That will be an error, because r
is a const int&
and cannot be modified. For now you can think of references as some sort of lightweight transparent "proxy" of an object. So if the "proxy" is const
, you cannot modify the object through the "proxy". However, it doesn't mean the original object cannot be modified.
don't understand that,because I think
const int &
should bind with aconst int
not aint
object
You are mistaken. A reference-to-const (const reference in short) doesn't mean that only const objects can be bound. It means that the object can not be modified through the reference.
What is not allowed would be to bind a non-const reference to a const object, because such reference could be used to modify the object which would break the constness.
the value of
r
can be changed, why?
The object that r
refers to was modified - which is OK because the object isn't const. r
still refers to the same object and the object was not modified using r
.
Having a const reference does not mean that the referred object can not change. It means that the object can not be changed using that reference.
If you wish to have an object that can not change, then that object itself has to be const. A const reference does not make the referred object const. The book has shown you how to create a const object:
const int b=a;
I have regarded reference as poiter mistakely,because they are similiar some time.
Indeed, references are very similar to pointers. Regarding the context of this question, they behave similarly. A demo:
int a=0;
const int *r=&a;
a=3;
cout<<*r<<endl;
An object cannot be modified "through" a const
reference or a pointer to const
of it. But a const
reference or pointer to const
doesn't impose any kind of run-time lock on the underlying object and (assuming it was not declared const
) the original declaration or any non-const
reference or pointer to non-const
can still be used to modify it.
Indeed if the object was not originally declared const
then const
-ness can be cast away and used to modify the underlying object.
Indeed even if the object was originally declared const
this may be possible (but is implementation dependent).
const
is a way of indicating that a function (or functions) shouldn't modify an object (either as an argument in our return value out or declaration).
It doesn't (in general) mean the object can't be modified through that or some other reference/pointer.
It also not possible to modify what a reference refers to. Or at least there is no valid way of doing it. In some circumstances it is possible in practice by jiggery pokery of obtaining an address of where a reference is held and modifying it like a pointer. That will usually fail in some circumstances because references are often optimized out of existence and because the compiler knows they 'cannot be modified' such violations might only be partially successful.
const
in this example only guarantee that a
can not be changed where r
is used. Usually this is used for functions that do not change input parameters like:
int doNotModifyFoo(const int &foo);