从 C++ 返回引用方法

发布于 2024-09-08 05:19:15 字数 486 浏览 5 评论 0原文

亲爱的朋友,我担心我是否在 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 技术交流群。

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

发布评论

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

评论(5

眼眸 2024-09-15 05:19:15

不会。 ref 仍然引用 me ,它将在调用结束时被销毁。

您应该返回结果的副本(不以 & 为前缀)。

MatrizEsparsa MatrizEsparsa::operator+(const MatrizEsparsa& outra) const {
    return MatrizEsparsa(outra.linhas(),outra.colunas());
}

我还添加了两个 const 说明符(对参数和方法),因为我怀疑在这种情况下需要修改 outra 或调用实例。 (我可能是错的,但是你的operator+会有一个奇怪的语义)

通过做你所做的事情,你只是让代码变得更加复杂。编译器可能很困惑,无法警告您可能的错误。

通常,当你必须使用巧妙的技巧来完成简单的事情时,就意味着出了问题。

No. ref still refers to me which will be destroyed at the end of the call.

You should return a copy of your result (not prefixed by &).

MatrizEsparsa MatrizEsparsa::operator+(const MatrizEsparsa& outra) const {
    return MatrizEsparsa(outra.linhas(),outra.colunas());
}

I also added two const specifiers (to the parameter and to the method) since I doubt outra or the calling instance need to be modified in this case. (I could be wrong, but then your operator+ 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.

无畏 2024-09-15 05:19:15

我认为您误解了您的操作员。

有 2 个:

struct Foo
{
  Foo& operator+=(Foo const&);
  Foo operator+(Foo const&) const;
};

正如您所注意到的,第一个返回对其自身的引用,第二个则不返回。

另外,一般来说,第二个应该写成自由函数。

Foo operator+(Foo const&, Foo const&);

这可以自动化,因为它很麻烦,使用 Boost.Operators:

struct Foo: boost::addable<Foo>
{
  Foo& operator+=(Foo const& rhs)
  {
    // add
    return *this;
  }
};

这个小小的 boost::addable 魔法将自动生成基于 Foo::operator+ 的 + 实现= 。

I think you're mistaking your operators.

There are 2:

struct Foo
{
  Foo& operator+=(Foo const&);
  Foo operator+(Foo const&) const;
};

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.

Foo operator+(Foo const&, Foo const&);

This can be automated, because it's cumbersome, using Boost.Operators:

struct Foo: boost::addable<Foo>
{
  Foo& operator+=(Foo const& rhs)
  {
    // add
    return *this;
  }
};

The little boost::addable magic will automatically generate the + implementation based on Foo::operator+=.

少女情怀诗 2024-09-15 05:19:15

这是不可接受的。这实际上是同样的问题:返回一个对本地对象的非常量引用,该对象在返回方法后将被销毁。

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.

尾戒 2024-09-15 05:19:15

不,您必须在此处返回一个值,最好是 const 值。请参阅《Effective C++》第 21 条。

我建议使用以下接口:

const MatrizEsparsa operator+(const MatrizEsparsa& left, const MatrizEsparsa& right);

请注意,所有内容要么是 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:

const MatrizEsparsa operator+(const MatrizEsparsa& left, const MatrizEsparsa& right);

Note that everything is either a const reference or a const value. Returning a const value is not as important as returning a value or declaring the parameters as const references, but the arguments of Scott Meyers have convinced me, although no one follows them.

前事休说 2024-09-15 05:19:15

您无法返回引用,因为您引用的对象将在您的控制之外被销毁。要么将“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.

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