从 C++ 返回引用方法
亲爱的朋友,我担心我是否在 C++ 中错误地使用了引用 在以下方法中,GCC 抱怨警告“返回了对局部变量‘me’的引用”
MatrizEsparsa& MatrizEsparsa::operator+(MatrizEsparsa& outra){
MatrizEsparsa me(outra.linhas(),outra.colunas());
return me;
}
但是,通过以下更改,警告消失了:
MatrizEsparsa& MatrizEsparsa::operator+(MatrizEsparsa& outra){
MatrizEsparsa me(outra.linhas(),outra.colunas());
MatrizEsparsa &ref = me;
return ref;
}
前一种方法(返回‘ref’变量)是否正确\可接受?
Dear friends, i'm concerned if i'm making a bad use of references in C++
In the following method GCC complains warning "reference to local variable ‘me’ returned"
MatrizEsparsa& MatrizEsparsa::operator+(MatrizEsparsa& outra){
MatrizEsparsa me(outra.linhas(),outra.colunas());
return me;
}
But, with the following changes the warning disappears:
MatrizEsparsa& MatrizEsparsa::operator+(MatrizEsparsa& outra){
MatrizEsparsa me(outra.linhas(),outra.colunas());
MatrizEsparsa &ref = me;
return ref;
}
Is the former method ( returning the 'ref' variable ) correct\acceptable ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不会。
ref
仍然引用me
,它将在调用结束时被销毁。您应该返回结果的副本(不以
&
为前缀)。我还添加了两个 const 说明符(对参数和方法),因为我怀疑在这种情况下需要修改 outra 或调用实例。 (我可能是错的,但是你的
operator+
会有一个奇怪的语义)通过做你所做的事情,你只是让代码变得更加复杂。编译器可能很困惑,无法警告您可能的错误。
通常,当你必须使用巧妙的技巧来完成简单的事情时,就意味着出了问题。
No.
ref
still refers tome
which will be destroyed at the end of the call.You should return a copy of your result (not prefixed by
&
).I also added two
const
specifiers (to the parameter and to the method) since I doubtoutra
or the calling instance need to be modified in this case. (I could be wrong, but then youroperator+
would have a weird semantic)By doing what you did, you just made the code more complex. The compiler probably was confused and couldn't warn you about your possible mistake.
Usually, when you have to use clever tricks to do simple things, it means something is wrong.
我认为您误解了您的操作员。
有 2 个:
正如您所注意到的,第一个返回对其自身的引用,第二个则不返回。
另外,一般来说,第二个应该写成自由函数。
这可以自动化,因为它很麻烦,使用 Boost.Operators:
这个小小的
boost::addable
魔法将自动生成基于Foo::operator+ 的
+
实现= 。I think you're mistaking your operators.
There are 2:
As you notice, the first returns a reference to itself, the second does not.
Also, in general, the second should be written as a free function.
This can be automated, because it's cumbersome, using Boost.Operators:
The little
boost::addable
magic will automatically generate the+
implementation based onFoo::operator+=
.这是不可接受的。这实际上是同样的问题:返回一个对本地对象的非常量引用,该对象在返回方法后将被销毁。
It is not acceptable. It is actually the same problem: returning a non-const reference to an local object that will be destroyed after returning the method.
不,您必须在此处返回一个值,最好是
const
值。请参阅《Effective C++》第 21 条。我建议使用以下接口:
请注意,所有内容要么是
const
引用,要么是const
值。返回 const 值并不像返回值或将参数声明为 const 引用那么重要,但 Scott Meyers 的论点已经说服了我,尽管没有人遵循它们。No, you must return a value here, ideally a
const
value. See Effective C++, Item 21.I suggest the following interface:
Note that everything is either a
const
reference or aconst
value. Returning aconst
value is not as important as returning a value or declaring the parameters asconst
references, but the arguments of Scott Meyers have convinced me, although no one follows them.您无法返回引用,因为您引用的对象将在您的控制之外被销毁。要么将“me”作为 MatrizEsparsa 的成员变量,以便它在函数执行后持续存在,要么返回一个指向该对象的指针或 boost smart_ptr。
不过,由于这是一个 + 运算符,您可能希望返回一个值,而不是对函数内部变量的引用。
You can't return the reference since the object you are referencing will get destroyed outside of your control. Either put "me" as a member variable of MatrizEsparsa so that it will persist after execution of the function else return a pointer, or a boost smart_ptr, that points to the object.
Seeing as this is a + operator though, you probably want to return a value rather than a reference to a variable internal to the function.