对新对象使用相同的引用 [英] Using the same Reference for a new object
问题描述
在搜索一些松散相关的东西时,我碰到了这个引号:
While searching for some loosely related stuff I bumped into this quote:
引用可以超过一个对象,
And a reference can outlive an object and be used to refer to a new object created at the same address.
从此 answer 。
现在,我一直都知道和工作的引用是不可变的,初始化一次和所有。
阅读上面的报价,由一个可能比我更有经验的人,让我想知道我是否缺少一些东西。
Now, I've always known and worked by references being immutable, initialized once and all that. Reading the above quote, by someone likely more experienced than I am, got me wondering if I'm missing something.
这句话是为了但是实际上不适用?
Was that sentence meant to be for the sake of completeness but practically inapplicable?
有一些模式或情况,人们会经历在特定内存地址登陆相同类型的新对象的痛苦只是做一个switcheroo作为参考? (这对我来说是非常危险的,更不用说在最好的时候复杂)。
Is there some pattern or circumstance where people would go through the pain of landing a new object of the same type in a specific memory address just to to do a switcheroo for a reference? (which seems supremely dangerous to me, not to mention convoluted at the best of times).
推荐答案
在 new
的上下文中的感觉。如果引用指向的对象是使用放置 new
创建的,则应该可以销毁对象并在同一位置创建一个新对象,放置 new
。我没有看到任何立即原因,而不是使用指针,而不是在那一刻,虽然。
I think this could only make sense in the context of a placement new
. If the object the reference points to was created with placement new
, it should be possible to destroy the object and create a new object in the same spot with placement new
again. I don't see any immediate reason not to use a pointer instead at the moment though.
这篇关于对新对象使用相同的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!