如果我已经超载了+和 = 运算符,我是否必须重载 +=?

发布于 2024-10-12 10:03:33 字数 173 浏览 3 评论 0原文

更一般地说,我应该重载哪些运算符?我知道重载的 += 是一件好事,因为扩展到 a = a + b 会创建一个临时对象,而重载的 += > 可以避免这种情况。 重载的 += 能否有效地使用我的重载运算符?

More generally, what operators should I be overloading? I know that an overloaded += is a good thing because the expansion to a = a + b creates a temporary object, whereas an overloaded += can avoid that. Will a += that is not overloaded effectively use my overloaded operators?

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

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

发布评论

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

评论(8

梦冥 2024-10-19 10:03:33

实现operator+的最佳方法有利于代码重用,

class Foo
{
    Foo(Foo const&); // Implemented

    Foo& operator+=(Foo const& rhs)
    {
        // Implement all the addition-specific code here
        return *this;
    }
};

然后

Foo operator+(Foo x, Foo const& y)
{
    x += y; return x;
}

如果您必须复制右侧的副本(由于实现特定的原因,通常情况并非如此),您可以通过通过 operator+=operator+ 的第二个参数中的值。

The best way to implement operator+, favoring code reuse, is

class Foo
{
    Foo(Foo const&); // Implemented

    Foo& operator+=(Foo const& rhs)
    {
        // Implement all the addition-specific code here
        return *this;
    }
};

and then

Foo operator+(Foo x, Foo const& y)
{
    x += y; return x;
}

If you have to do a copy of the right hand side (for implementation specific reasons, it is usually not the case), you can pass by value in both operator+= and the second argument of operator+.

一杯敬自由 2024-10-19 10:03:33

您不必这样做。编译器不会抛出错误或警告,告诉您所做的事情是错误的。

然而,在绝大多数情况下你确实应该这样做。编译器不会将 a += b 转换为 a = a + b

You don't have to. The compiler won't throw an error or warning telling you that what you've done is wrong.

However, you really really should in the vast majority of cases. The compiler will NOT transform a += b into a = a + b.

骑趴 2024-10-19 10:03:33

如果你不重载+=,它就不会被定义;你将无法使用它。

我个人建议您提供它,因为如果 += 可用,那么任何人都可能期望 += 可用。

我还建议您使用 Boost.Operators为了使应该根据其他运算符自动生成的运算符重载。

If you don't overload += it won't be defined; you won't be able to use it.

I'd personally suggest you to provide it, since whoever would probably expect that += is available, if + and = are.

I'd also suggest you to use Boost.Operators in order to overload the operators that should be automatically generated in terms of others.

寒冷纷飞旳雪 2024-10-19 10:03:33

最好先实现 operator+=operator=,因为根据它们实现 operator+ 很简单。 IIRC 这是 Scott Meyers 的 Effective C++ 版本。

It is better to implement operator+= and operator= first, as implementing operator+ in terms of them is trvial. IIRC this is in Effective C++ by Scott Meyers.

关于从前 2024-10-19 10:03:33

是的,它是一个不同的运算符,因此您必须重载它。您可以(并且可能应该)在重载函数内使用其他运算符(例如“+”)。

Yes, it's a different operator, so you will have to overload it. You can (and probably should) use your other operators (e.g. '+') inside the overloaded function.

小红帽 2024-10-19 10:03:33

你必须重载+=。

然后,您可以重载它来执行完全不同的操作,但不建议这样做。 ;-)。

You have to overload +=.

Then, you could overload it to do something completely different, but it is not advised. ;-).

独闯女儿国 2024-10-19 10:03:33

当你说“我必须……”时,只有当你需要使用它时。一般来说,最好先实现operator+=,然后再根据它实现operator+,因为大多数人都会弄错operator+(不要将其设为const,忘记返回副本...)

std::accumulate 使用 a = a + b如果你所做的只是编写一些东西来适应它,那么 += 本身不会有帮助,尽管你仍然可以根据它来实现运算符+。

当然,您可能决定不允许用户使用 += 修改您的对象(尽管您不介意他们制作副本)。

When you say "do I have to..", only if you need to use it. It is better generally to implement operator+= and then implement operator+ in terms of it, because most people get operator+ wrong (don't make it const, forget to return a copy...)

std::accumulate uses a = a + b and if all you are doing is writing something to fit it, += won't help on its own, although you can still then implement operator+ in terms of it.

Of course you might decide you don't want to allow users to modify your objects with += (whereas you don't mind them making copies).

泼猴你往哪里跑 2024-10-19 10:03:33

不,你不必这样做。但不要认为因为你重载了+和=,你就免费拥有了+=。当然,你不会仅仅因为你已经超载了 - 并且>就认为这一点。你还有->操作员?

No you DO NOT HAVE TO. But just don't think that because you overloaded + and = you have += for free. Shurely you do not assume that just because you've overloaded - and > you also have -> operator?

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