分配一个 C++引用被破坏的东西?

发布于 2024-08-08 08:03:31 字数 304 浏览 1 评论 0原文

因此,我正在查看一些代码,我看到了这一点:

class whatever 
{
public:
    void SomeFunc(SomeClass& outVal)
    {
        outVal = m_q.front();
        m_q.pop();
    }

private:
    std::queue<SomeClass> m_q;
};

这似乎不再是 outVal 的有效引用...但是,它似乎有效。

我以前也在其他代码中看到过这个,这有效吗?谢谢

So I'm looking through some code, and I see this:

class whatever 
{
public:
    void SomeFunc(SomeClass& outVal)
    {
        outVal = m_q.front();
        m_q.pop();
    }

private:
    std::queue<SomeClass> m_q;
};

This doesn't seem like outVal would be a valid reference any more... However, it appears to work.

I've seen this in other code before too, is this valid? Thanks

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

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

发布评论

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

评论(3

成熟稳重的好男人 2024-08-15 08:03:31

请记住,引用与指针不同:它们在创建后不能反弹。这意味着如果我

int a;
int b;
int &c = a;

在整个范围内这样做,对 c 的赋值实际上意味着对 a 的赋值。因此

int a = 2;
{
   int b = 3;
   int &c = a;
   c = b;
   b = -5;
}
printf("%d",a); // prints "3".

,在这种情况下,引用不会指向已删除的对象。相反,m_q.front() 的返回值通过赋值运算符复制到任何 outVal 引用中。

Remember that references are not like pointers: they cannot be rebound after their creation. That means that if I do

int a;
int b;
int &c = a;

Then throughout that scope, an assignment to c will actually mean an assignment to a. So,

int a = 2;
{
   int b = 3;
   int &c = a;
   c = b;
   b = -5;
}
printf("%d",a); // prints "3".

So, in this case, the reference is not being pointed at a deleted object. Rather, the return value of m_q.front() is copied into whatever outVal references, via the assignment operator.

她如夕阳 2024-08-15 08:03:31

这是有效的。您没有重新设置 outVal 引用来引用 m_q.front(),这不是引用支持的内容,而是分配 m_q.front()outVal 引用的变量(实际上是左值)。

  SomeClass c;
  someWhatever.SomeFunc(c);

可以被认为是这样的行为:

  SomeClass c;
  c = someWhatever.m_q.front();
  someWhater.m_q.pop();

It's valid. You are not reseating the outVal reference to refer to m_q.front(), that is not something supported by references, instead you are assigning m_q.front() to the variable (actually lvalue) that outVal refers to.

  SomeClass c;
  someWhatever.SomeFunc(c);

Can be thought of as behaving like:

  SomeClass c;
  c = someWhatever.m_q.front();
  someWhater.m_q.pop();
南笙 2024-08-15 08:03:31

我之前的回复里写的完全是废话。 (无论谁支持我的原始回复,请收回它:)

在这个例子中,引用没有绑定到一个即将死亡的对象,而是将前面的对象的值复制到另一个对象(由引用引用)。副本继续独立于队列而存在,队列前端被破坏的事实不会对副本产生不利影响。

请参阅 Crashworks 回复以获得对此处发生的情况的详细解释。

What I wrote in my previous reply was complete nonsense. (Whoever upvoted my original response, please take it back :)

In this example the reference is not bound to a dying object, but rather the value of the object in the front is copied to another object (referred to by the reference). The copy continues to exist independently of the queue an the fact that the front of the queue is destroyed has no adverse effects on the copy.

Please, refer to Crashworks reply for a great explanation of what is happening here.

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