简体   繁体   English

为什么const int和bind绑定到int?

[英]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 ; 在C ++入门中,我发现const int &可以与int对象绑定。我不明白,因为我认为const int &应该与const int绑定而不是int对象, int对象可以改变,本书当const int & object与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: 我们可以使用b作为临时值,因此上面等于:

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: 但我认为这本书是不正确的,如果有一个暂时的,因为像b之间存在arr不能改变,但是当我调试在Visual Studio中的以下编码,我发现这是不对的:

int a=0;
const int &r=a;
a=3;
cout<<r<<endl;

The output is that r=3; 输出是r=3; the value of r can be changed, why? r的值可以改变,为什么? I don't understand that. 我不明白。

don't understand that,because I think const int & should bind with a const int not a int object 不明白,因为我认为const int &应该与const int绑定而不是int对象

You are mistaken. 你误会了。 A reference-to-const (const reference in short) doesn't mean that only const objects can be bound. 引用const(简称const引用)并不意味着只能绑定const对象。 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. 不允许的是将非const引用绑定到const对象,因为这样的引用可用于修改将破坏const的对象。

the value of r can be changed, why? r的值可以改变,为什么?

The object that r refers to was modified - which is OK because the object isn't const. r引用的对象被修改 - 这是正常的,因为该对象不是const。 r still refers to the same object and the object was not modified using r . r仍指向同一个对象,并且未使用r修改对象。

Having a const reference does not mean that the referred object can not change. 具有const引用并不意味着引用的对象不能更改。 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. 如果你希望有一个无法改变的对象,那么该对象本身必须是const。 A const reference does not make the referred object const. const引用不会使引用的对象成为const。 The book has shown you how to create a const object: 这本书向您展示了如何创建一个const对象:

const int b=a;

I have regarded reference as poiter mistakely,because they are similiar some time. 我错误地认为引用是poiter,因为它们在某个时候是相似的。

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. const在这个例子中只保证a ,其中不能被改变r被使用。 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. 不能通过const引用或指向const的指针来修改对象。 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. const引用或指向const指针不会对底层对象施加任何类型的运行时锁定(假设它未被声明为const )原始声明或任何非const引用或指向非const指针仍然可以用来修改它。

Indeed if the object was not originally declared const then const -ness can be cast away and used to modify the underlying object. 实际上,如果对象最初未被声明为const那么const -ness可以被抛弃并用于修改底层对象。

Indeed even if the object was originally declared const this may be possible (but is implementation dependent). 实际上,即使对象最初被声明为const这也许是可能的(但是依赖于实现)。

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). const是一种表示函数(或函数)不应修改对象的方式(作为返回值输出或声明中的参数)。

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. 在某些情况下,在实践中,jiggery pokery可能获得一个引用所在的地址并像指针一样修改它。 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. 在C ++中,您可以使用引用和/或const指针来引用非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. 当然,由于x不是常数,因此可以改变。 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 . 但是,通过对非const对象进行const引用,您基本上所说的是: 我不会通过此引用/指针更改此对象 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; 输出是r = 3; the value of r can be changed, why? r的值可以改变,为什么? I don't understand that. 我不明白。

The const only applies to the reference r . const仅适用于参考r It means that you can't change whatever r binded to via r . 这意味着,无论你不能改变r通过绑定到r You can't do: 你做不到:

r = 3;

That will be an error, because r is a const int& and cannot be modified. 这将是一个错误,因为r是一个const int&并且无法修改。 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". 因此,如果“代理”是const ,则无法通过“代理”修改对象。 However, it doesn't mean the original object cannot be modified. 但是,这并不意味着原始对象无法修改。

Basically, const int & r promises not to mutate the value it's referencing. 基本上, const int & r承诺不会改变它引用的值。 This is a stronger guarantee than int & . 这是一个比int &更强的保证。 So, it's possible to refer to an int using a const int & reference. 因此,它可能是指一个int使用const int &参考。 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. 但是,反之亦然,如果你试图得到一个int &const int值的引用,它会导致编译器错误,因为值本身是不可变的,并且你试图得到一个可变引用这个不可变的价值。 The operations possible on the int & reference are a superset of what's possible on const int value. int & reference上可能的操作是const int值可能的超集。

The value of the variable a which is 0 initially is holded at memory. 最初为0的变量a的值保留在存储器中。

If you write later: a=5 , it'll set that value in memory to 5 . 如果你稍后写: a=5 ,它会将内存中的值设置为5

When you write a reference const int & r = a; 当你写一个引用const int & r = a; you are only saying that r should access that same place in memory where 5 is being hold. 你只是说r应该访问内存中保持5相同位置。 在此输入图像描述

Because a is not const it can modify the value. 因为a不是const所以可以修改该值。

This is basically how it works. 基本上就是它的工作原理。

There are two things: Const and reference. 有两件事:Const和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 const引用在C ++中用类似的代码表示

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 . 在这里,你不能用i来改变j的值。 However this does not make the memory location j to be constant.You can peacefully change the value of j . 但是,这并不会使内存位置j保持不变。您可以和平地更改j的值。

j =3
//but i = 3 will raise an error saying 
//assignment of read-only reference ‘i’

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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