xcode 返回对本地临时对象的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28275246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:35:34  来源:igfitidea点击:

Returning reference to local temporary object

c++iosxcodeobjective-c++irrlicht

提问by regetskcob

This code

这段代码

virtual const core::matrix4& getViewMatrixAffector() const {return core::matrix4();};

results with a warning telling me "Returning reference to local temporary object"...

结果有一个警告告诉我“返回对本地临时对象的引用”......

How to solve this warning?

如何解决这个警告?

As mentioned below i tried to remove the '&'... Error

如下所述,我试图删除“&”... 错误

采纳答案by Angew is no longer proud of SO

Since you're not in control of the return type, you must make sure you return a valid object and not just a temporary. One solution would be a function-local static variable:

由于您无法控制返回类型,因此必须确保返回有效对象而不仅仅是临时对象。一种解决方案是函数局部静态变量:

virtual const core::matrix4& getViewMatrixAffector() const
{
  static const core::matrix4 val;
  return val;
};

If you find yourself doing this in many functions (with the same type of the variable), make vala (suitably renamed) static member of the class.

如果您发现自己在许多函数(具有相同类型的变量)中执行此操作,请为该类创建val一个(适当重命名的)静态成员。

回答by Cássio Renan

When you create an object as a local temporary, it is destroyed as soon as the function's scope ends. In turn, you should never return a reference to it, as this would yield Undefined Behaviour. Consider returning it by value, or returning a smart pointer to an object on the free store.

当您创建一个对象作为本地临时对象时,一旦函数的作用域结束,它就会被销毁。反过来,你不应该返回对它的引用,因为这会产生未定义的行为。考虑按值返回它,或者返回一个指向空闲存储中对象的智能指针。

回答by 10100111001

If it really is a local object you are returning a reference to, then you shouldn't do it, because that object will not be valid once the function getViewMatrixAffector() returns.

如果它确实是您要返回引用的本地对象,那么您不应该这样做,因为一旦函数 getViewMatrixAffector() 返回,该对象将无效。

回答by TartanLlama

When you return by reference, as in core::matrix4&, you need an object which will still be around when the function returns. In your case, you are returning a "local temporary object", which is destructed after that function exits. In order to fix this, you need to return by value, like so:

当您通过引用返回时,如在 中core::matrix4&,您需要一个在函数返回时仍然存在的对象。在您的情况下,您将返回一个“本地临时对象”,该对象在该函数退出后被破坏。为了解决这个问题,您需要按值返回,如下所示:

virtual const core::matrix4 getViewMatrixAffector() const {return core::matrix4();};
//                        ^ no '&'

回答by K. Arenhold

Intel C++ 14.0.3.202 gives this warning even if the reference is valid outside the function. This is just a bug I witnessed in this version, but it may also appear in others. If you use this version: just mask the warning out by wrapping your function this way:

即使引用在函数外有效,英特尔 C++ 14.0.3.202 也会发出此警告。这只是我在这个版本中目睹的一个错误,但它也可能出现在其他版本中。如果您使用此版本:只需通过以这种方式包装您的函数来掩盖警告:

#pragma warning(push)
#pragma warning(disable:473)
( your function definition )
#pragma warning(pop)

I′m not sure if 473 is the index of this warning - but you see the correct one in the compuler′s messages.

我不确定 473 是否是此警告的索引 - 但您会在计算机的消息中看到正确的索引。