为什么我可以将现有引用分配给 C++ 中的文字值?

发布于 2024-07-12 02:57:38 字数 462 浏览 6 评论 0原文

请考虑以下情况:

int ival = 1.01;
int &rval = 1.01; // error: non-const reference to a const value.
int &rval = ival;

rval = 1.01;

&rval 第一次对文字值的赋值按预期失败。 如果我注释掉该行,代码就会编译并运行。 我理解为什么初始化失败,但我很困惑为什么对 rval 的分配在最后一行起作用。 我认为不允许将引用分配给文字值。

编辑:感谢您的快速解答。 我很想删除它以隐藏我的耻辱,但我想我会把它留在这里,这样其他人就可以指指点点并笑了。

在我自己的辩护中,我正在完成一本书(C++ Primer)中的练习,这个问题是关于引用初始化的。 尽管如此,一开始就完全忽视了参考点还是相当令人尴尬的。 :)

Consider the following:

int ival = 1.01;
int &rval = 1.01; // error: non-const reference to a const value.
int &rval = ival;

rval = 1.01;

The first assignment of &rval to a literal value fails as expected. If I comment out that line the code compiles and runs. I understand why the initialization fails, but I'm confused why the assignment to rval works in the last line. I didn't think it was allowed to assign a reference to a literal value.

EDIT: Thanks for the quick answers. I'm tempted to delete this to hide my shame, but I think I'll leave it here so everyone else can point and laugh.

In my own defense, I'm working through the exercises in a book (C++ Primer) and this problem is about reference initialization. Still, it's pretty embarrassing to have so completely overlooked the point of a reference in the first place. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回首观望 2024-07-19 02:57:38

ival 不是文字值,1.01 是文字值。 它已被复制到 ival,这是一个变量,它绝对可以将其引用分配给另一个变量。

ival isn't a literal value, 1.01 is the literal value. It's been copied to ival which is a variable, which most definitely can have it's references assigned to another variable.

巷子口的你 2024-07-19 02:57:38

引用初始化后,它是其初始化的别名。 一旦引用被初始化,您就无法更改该引用所指的内容。 但是,您可以更改引用引用的“事物”(如果引用不是 const)。

最后一行实际上将变量 ival 设置为 1.01(再次)。

After the reference is initialized, it is an alias for what it was initialized to. Once a reference is initialized, you cannot change what the reference refers to. However you can change the 'thing' the reference refers to (if the reference is not const).

The last line is in effect setting the variable ival to 1.01 (again).

伏妖词 2024-07-19 02:57:38

您可能会惊讶地发现您可以执行以下操作:

const int& x = 42;        // No error!

C++ 确实允许使用文字值(或其他右值)初始化const 引用。 实际发生的情况是,编译器通过复制 RHS 创建一个临时文件,并初始化引用以引用该临时文件。 编译器确保临时变量保持活动状态,直到作用域退出(即,在这种情况下,每当 x 超出作用域时)。

请注意,这在初始化类的成员变量时不起作用。

棘手吧? :)

Here is something you may be surprised to find that you can do though:

const int& x = 42;        // No error!

C++ does allow initialising a const reference with a literal value (or other rvalue). What actually happens is that the compiler creates a temporary by copying the RHS, and initialises the reference to refer to that temporary. The compiler ensures that the temporary stays alive until scope exit (i.e whenever x goes out of scope in this case).

Note that this doesn't work when initialising member variables of a class.

Tricky huh? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文