const对unsigned int的casted int的引用

时间:2022-01-07 17:07:16

I am having some trouble understanding the behaviour in this snippet:

我在理解此代码段中的行为时遇到了一些麻烦:

unsigned int i = 2;
const int &r = i;
std::cout << r << "\n";
i = 100;
std::cout << r << "\n";

The first print statement gives 2 as I expect, but when I change the value of the referenced variable, it is not reflected in the reference. The second print statement also gives 2, but I think it should give 100?

第一个print语句按预期给出2,但是当我更改引用变量的值时,它不会反映在引用中。第二个印刷声明也给出2,但我认为它应该给100?

If I make variable i into type int instead of unsigned int, it works as I expect. What is going on here?

如果我将变量i变成int类型而不是unsigned int,它就像我期望的那样工作。这里发生了什么?

Live example

2 个解决方案

#1


33  

You can only have a reference to an object of the same type.

您只能引用相同类型的对象。

You cannot have an int reference to an unsigned int.

您不能对unsigned int进行int引用。

What is happening here is, essentially:

这里发生的事情基本上是:

const int &r = (int)i;

A new int temporary gets constructed, a new temporary object, and a const reference is bound to it.

构造一个新的int临时对象,绑定一个新的临时对象和一个const引用。

Using your debugger, you should be able to observe the fact that the reference is referring to a completely different object:

使用调试器,您应该能够观察到引用指的是一个完全不同的对象:

(gdb) n
6   const int &r = i;
(gdb) 
7   std::cout << r << "\n";
(gdb) p i
$1 = 2
(gdb) p &i
$2 = (unsigned int *) 0x7fffffffea0c
(gdb) p &r
$3 = (const int *) 0x7fffffffe9fc
(gdb) q

#2


9  

The second print statement also gives 2, but I think it should give 100?

第二个印刷声明也给出2,但我认为它应该给100?

Because a temporary int is created here.

因为在这里创建了临时int。

For const int &r = i;, i (unsigned int) needs to be converted to int at first, means a temporary int will be created and then be bound to r (temporary could be bound to lvalue reference to const), it has nothing to do with the original variable i any more.

对于const int&r = i;,i(unsigned int)首先需要转换为int,意味着将创建临时int然后绑定到r(临时可以绑定到对const的lvalue引用),它什么都没有与原始变量i有关。

If I make variable i into type int instead of unsigned int, it works as I expect.

如果我将变量i变成int类型而不是unsigned int,它就像我期望的那样工作。

Because no conversion and temporary is needed, i could be bound to r directly.

因为不需要转换和临时转换,所以我可以直接绑定r。

#1


33  

You can only have a reference to an object of the same type.

您只能引用相同类型的对象。

You cannot have an int reference to an unsigned int.

您不能对unsigned int进行int引用。

What is happening here is, essentially:

这里发生的事情基本上是:

const int &r = (int)i;

A new int temporary gets constructed, a new temporary object, and a const reference is bound to it.

构造一个新的int临时对象,绑定一个新的临时对象和一个const引用。

Using your debugger, you should be able to observe the fact that the reference is referring to a completely different object:

使用调试器,您应该能够观察到引用指的是一个完全不同的对象:

(gdb) n
6   const int &r = i;
(gdb) 
7   std::cout << r << "\n";
(gdb) p i
$1 = 2
(gdb) p &i
$2 = (unsigned int *) 0x7fffffffea0c
(gdb) p &r
$3 = (const int *) 0x7fffffffe9fc
(gdb) q

#2


9  

The second print statement also gives 2, but I think it should give 100?

第二个印刷声明也给出2,但我认为它应该给100?

Because a temporary int is created here.

因为在这里创建了临时int。

For const int &r = i;, i (unsigned int) needs to be converted to int at first, means a temporary int will be created and then be bound to r (temporary could be bound to lvalue reference to const), it has nothing to do with the original variable i any more.

对于const int&r = i;,i(unsigned int)首先需要转换为int,意味着将创建临时int然后绑定到r(临时可以绑定到对const的lvalue引用),它什么都没有与原始变量i有关。

If I make variable i into type int instead of unsigned int, it works as I expect.

如果我将变量i变成int类型而不是unsigned int,它就像我期望的那样工作。

Because no conversion and temporary is needed, i could be bound to r directly.

因为不需要转换和临时转换,所以我可以直接绑定r。