gpt4 book ai didi

c++ - `obj.operator+=(rhs)` 与 `obj += rhs`

转载 作者:行者123 更新时间:2023-12-01 14:24:50 27 4
gpt4 key购买 nike

class test
{
public:
int i = 0;
test& operator+=(const test &rhs)
{
i += rhs.i;
return *this;
}
};

int main()
{
test t;
test rhs;
rhs.i = 10;
// what's the difference betwen these 2?
t.operator+=(rhs);
t += rhs;
}

这里t.operator+=(rhs);t += rhs;有区别吗?我一直用的是后者,从来没有想过前者。使用前者比使用后者有什么优势吗?

最佳答案

在大多数情况下,没有区别。当你写:

t += rhs

编译器会将其处理为:

t.operator+=(rhs)

因此,这两个调用只是同一调用的不同语法。

关于c++ - `obj.operator+=(rhs)` 与 `obj += rhs`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63532769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com