对非常量对象的 const 引用
下面,在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。
引用只是现有对象的别名。
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 referencer
.* 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 aconst
object.* Of course, you are still free to modify the object via
x
. Any changes will also be visible viar
, because they refer to the same object.右侧
是一个左值,
x
的类型与引用的类型相同(忽略 cv 限定)。在这些情况下,引用直接附加到x
,不会创建临时文件。至于“这是如何工作的”......我不明白是什么引起了你的问题。它以最直接的方式工作:引用直接附加到
x
。仅此而已。你不能这样做,
因为它立即违反了常量正确性的规则。
In
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 tox
, 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
because it immediately violates the rules of const-correctness.
对引用(&)的理解回答了这个问题。
引用只是分配给它的变量的别名。const 是
编译器对声明为 const 的变量施加的约束
在这种情况下,它是对非常量变量的常量引用。因此,您不能使用引用变量 r 更改 x 的数据(仅充当只读)。但是您仍然可以通过修改 x 来更改数据 x
在这种情况下,对 const 成员的非 const 引用是没有意义的。您是说引用可以允许您编辑 const 成员(这是不可能的)..
因此,如果您想为 const 成员创建引用,它必须像您提到的第一种情况一样
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
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
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