gpt4 book ai didi

c++ - operator= 重载返回参数而不是 *this

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:53:19 26 4
gpt4 key购买 nike

我有一个理论问题:

通常,在 operator= 实现中,它返回 *this。但是,如果我们改为返回 *other,会发生什么情况,其中 other 是赋值的右侧?

谢谢

最佳答案

返回的原因 *这是为了启用此表单中的赋值

a = b = c

这是一样的

a.operator=( b.operator=(c))

如果返回 other,编译器将无法编译此类赋值。

关于c++ - operator= 重载返回参数而不是 *this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24434971/

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