从函数返回对局部变量的 const 引用

发布于 2024-08-06 01:05:01 字数 665 浏览 10 评论 0原文

我对从函数返回对局部变量的引用有一些疑问:

class A {
public:
    A(int xx)
    : x(xx)
    {
        printf("A::A()\n");
    }
};

const A& getA1()
{
    A a(5);
    return a;
}

A& getA2()
{
    A a(5);
    return a;
}

A getA3()
{
    A a(5);
    return a;
}

int main()
{
    const A& newA1 = getA1(); //1
    A& newA2 = getA2(); //2
    A& newA3 = getA3(); //3
}

我的问题是 =>

  1. getA1() 的实现是否正确? 我觉得它不正确,因为它返回的是局部变量或临时变量的地址。

  2. main (1,2,3) 中的哪些语句会导致未定义的行为?

  3. const A&中newA1 = getA1(); 标准是否保证 const 引用的临时绑定在该引用超出范围之前不会被销毁?

I have some questions on returning a reference to a local variable from a function:

class A {
public:
    A(int xx)
    : x(xx)
    {
        printf("A::A()\n");
    }
};

const A& getA1()
{
    A a(5);
    return a;
}

A& getA2()
{
    A a(5);
    return a;
}

A getA3()
{
    A a(5);
    return a;
}

int main()
{
    const A& newA1 = getA1(); //1
    A& newA2 = getA2(); //2
    A& newA3 = getA3(); //3
}

My questions are =>

  1. Is the implementation of getA1() correct?
    I feel it is incorrect as it is returning the address of a local variable or temporary.

  2. Which of the statements in main (1,2,3) will lead to undefined behavior?

  3. In const A& newA1 = getA1(); does the standard guarantee that a temporary bound by a const reference will not be destroyed until the reference goes out of scope?

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

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

发布评论

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

评论(4

生生漫 2024-08-13 01:05:01

1. getA1() 实现正确吗?我觉得它不正确,因为它返回局部变量或临时变量的地址。

程序中唯一正确的 getAx() 版本是 getA3()。无论您以后如何使用它们,其他两个都有未定义的行为。

2. main(1,2,3) 中的哪些语句会导致未定义的行为?

从某种意义上说,他们都不是。对于 1 和 2,未定义的行为是函数体的结果。对于最后一行,newA3 应该是一个编译错误,因为您无法将临时值绑定到非常量引用。

3.在 const A& 中newA1 = getA1(); 标准保证由 const 临时绑定
在引用超出范围之前,引用不会被销毁?

不。下面是一个示例:

A const & newConstA3 = getA3 ();

这里,getA3() 返回一个临时变量,该临时变量的生命周期现在绑定到对象 newConstA3。换句话说,临时对象将一直存在,直到 newConstA3 超出范围。

1. Is getA1() implementation correct ? I feel it is incorrect as it is returning address of local variable or temporary.

The only version of getAx() that is correct in your program is getA3(). Both of the others have undefined behaviour no matter how you use them later.

2. Which of the statements in main ( 1,2,3) will lead to undefined behavior ?

In one sense none of them. For 1 and 2 the undefined behaviour is as a result of the bodies of the functions. For the last line, newA3 should be a compile error as you cannot bind a temporary to a non const reference.

3. In const A& newA1 = getA1(); does standard guarantees that temporary bound by a const
reference will not be destroyed until the reference goes out of scope?

No. The following is an example of that:

A const & newConstA3 = getA3 ();

Here, getA3() returns a temporary and the lifetime of that temporary is now bound to the object newConstA3. In other words the temporary will exist until newConstA3 goes out of scope.

很糊涂小朋友 2024-08-13 01:05:01

Q1:是的,这是一个问题,请参阅问题2的答案。

Q2:1和2是未定义的,因为它们引用了getA1和getA2堆栈上的局部变量。这些变量超出了范围,不再可用,更糟糕的是,随着堆栈不断变化,这些变量可能会被覆盖。 getA3 可以工作,因为创建了返回值的副本并将其返回给调用者。

问题 3:不存在这样的保证来看到问题 2 的答案。

Q1: Yes, this is a problem, see answer to Q2.

Q2: 1 and 2 are undefined as they refer to local variables on the stack of getA1 and getA2. Those variables go out of scope and are no longer available and worse can be overwritten as the stack is constantly changing. getA3 works since a copy of the return value is created and returned to the caller.

Q3: No such guarantee exists to see answer to Q2.

著墨染雨君画夕 2024-08-13 01:05:01

我认为主要问题是你根本没有返回临时变量,你应该

return A(5);

而不是

A a(5);
return a;

否则你返回的是局部变量地址,而不是临时变量。而临时到 const 的引用仅适用于临时变量。

我认为它的解释在这里:
临时为 const参考

I think the main problem is that you are not returning temporaries at all, you should

return A(5);

rather than

A a(5);
return a;

Otherwise you are returning local variable address, not temporary. And the temporary to const reference only works for temporaries.

I think its explained here:
temporary to const reference

把昨日还给我 2024-08-13 01:05:01

如果您在 VC6 上编译它,您将收到此警告

******编译器警告(级别 1)C4172
返回局部变量或临时变量的地址
函数返回局部变量或临时对象的地址。当函数返回时,局部变量和临时对象会被销毁,因此返回的地址无效。 *****

在测试这个问题时,我发现了有趣的事情(给定的代码在 VC6 中工作):

 class MyClass
{
 public:
 MyClass()
 {
  objID=++cntr;
 }
MyClass& myFunc()
{
    MyClass obj;
    return obj;
}
 int objID;
 static int cntr;
};

int MyClass::cntr;

main()
{
 MyClass tseadf;
 cout<<(tseadf.myFunc()).objID<<endl;

}

If you will compile this on VC6 you will get this warning

******Compiler Warning (level 1) C4172
returning address of local variable or temporary
A function returns the address of a local variable or temporary object. Local variables and temporary objects are destroyed when a function returns, so the address returned is not valid.******

While testing for this problem i found interesting thing (given code is working in VC6):

 class MyClass
{
 public:
 MyClass()
 {
  objID=++cntr;
 }
MyClass& myFunc()
{
    MyClass obj;
    return obj;
}
 int objID;
 static int cntr;
};

int MyClass::cntr;

main()
{
 MyClass tseadf;
 cout<<(tseadf.myFunc()).objID<<endl;

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