简体   繁体   中英

Why can const int& bind to an int?

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 const int not a int object, the int object can change, the book explain this question for that when the const int & object bind with int ; there is a temporary object between the two, for example:

int a=0;
const int &r=a;

We can use b as the temporary value, so above is equal that:

const int b=a;
const int &r=b;

But I think the book is not right, because if there is a temporary like b existing between a and r ,the value of r can't be changed, but when I debug the following coding in visual studio, I found it is not right:

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.

don't understand that,because I think const int & should bind with a const int not a int 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;

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);

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.

In C++, you can refer to non-const objects with references and/or pointers to const.

int x = 10;
const int& r = x; //OK
const int* p = &x; //OK

Of course, since x is not constant it can be changed. However, what you're basically saying by having a const reference to a non-const object is: I will not change this object via this reference/pointer . You still can change the object directly or through other references or pointers.

Think of it as a read-only handle. Yes, the object itself may be mutable, but in many cases you may be willing to acquire and/or provide only a read-only access to that otherwise mutable variable.

 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.

Basically, const int & r promises not to mutate the value it's referencing. This is a stronger guarantee than int & . So, it's possible to refer to an int using a const int & reference. But you may not modify it. It's a subset of the operations possible on the value. However, it's not true the other way around, if you were trying to get a int & reference to an const int value, it would result in a compiler error because the value itself is immutable, and you are trying to get a mutable reference to this immutable value. The operations possible on the int & reference are a superset of what's possible on const int value.

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’

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM