对非常量对象的 const 引用

发布于 2024-12-04 07:15:40 字数 341 浏览 1 评论 0原文

下面,在将 const 引用用于非 const 对象之前,是否会创建一个临时对象?

const int y = 2000;
const int &s = y // ok, const reference to const object.

int x = 1000;
const int &r = x; // any temporary copy here?

如果没有,那么这是如何运作的?

   const int z = 3000;
   int &t = z // ok, why can't you do this?

In the following, would there be a temporary object created before const reference is used to a non-const object?

const int y = 2000;
const int &s = y // ok, const reference to const object.

int x = 1000;
const int &r = x; // any temporary copy here?

If no then, how does this work?

   const int z = 3000;
   int &t = z // ok, why can't you do this?

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

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

发布评论

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

评论(3

Hello爱情风 2024-12-11 07:15:40

不。

引用只是现有对象的别名。 const 由编译器强制执行;它只是检查您是否尝试通过引用 r 修改对象。* 这不需要创建副本。

鉴于 const 只是编译器强制执行“只读”的指令,那么最终示例无法编译的原因应该立即显而易见。如果您可以通过对 const 对象使用非 const 引用来轻松规避它,那么 const 将毫无意义。

* 当然,您仍然可以通过x自由修改对象。任何更改也可以通过 r 看到,因为它们引用同一个对象。

No.

A reference is simply an alias for an existing object. const is enforced by the compiler; it simply checks that you don't attempt to modify the object through the reference r.* This doesn't require a copy to be created.

Given that const is merely an instruction to the compiler to enforce "read-only", then it should be immediately obvious why your final example doesn't compile. const would be pointless if you could trivially circumvent it by taking a non-const ref to a const object.

* Of course, you are still free to modify the object via x. Any changes will also be visible via r, because they refer to the same object.

阿楠 2024-12-11 07:15:40

右侧

int x = 1000;
const int &r = x;

是一个左值x 的类型与引用的类型相同(忽略 cv 限定)。在这些情况下,引用直接附加到x,不会创建临时文件。

至于“这是如何工作的”......我不明白是什么引起了你的问题。它以最直接的方式工作:引用直接附加到x。仅此而已。

你不能这样做,

const int z = 3000;
int &t = z;

因为它立即违反了常量正确性的规则。

In

int x = 1000;
const int &r = x;

the right-hand side is an lvalue and the type of x is the same as the type of the reference (ignoring cv-qualifications). Under these circumstances the reference is attached directly to x, no temporary is created.

As for "how does this work"... I don't understand what prompted your question. It just works in the most straighforward way: the reference is attached directly to x. Nothing more to it.

You can't do

const int z = 3000;
int &t = z;

because it immediately violates the rules of const-correctness.

人│生佛魔见 2024-12-11 07:15:40

对引用(&)的理解回答了这个问题。

引用只是分配给它的变量的别名。const 是

编译器对声明为 const 的变量施加的约束

int x = 1000;
const int &r = x;

在这种情况下,它是对非常量变量的常量引用。因此,您不能使用引用变量 r 更改 x 的数据(仅充当只读)。但是您仍然可以通过修改 x 来更改数据 x

const int z = 3000;
int &t = z

在这种情况下,对 const 成员的非 const 引用是没有意义的。您是说引用可以允许您编辑 const 成员(这是不可能的)..

因此,如果您想为 const 成员创建引用,它必须像您提到的第一种情况一样

const int z = 3000;
const int &t = z;

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000;
const int &r = x;

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000;
int &t = z

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

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