【发布时间】:2017-10-05 01:33:15
【问题描述】:
我无法理解以下内容:
假设我正在创建一个重载的赋值运算符。函数应该是这样的:
MyObject& MyObject::operator=(const &rhs)
{
// code to make this work
return *this;
}
如果 'this' 已经是一个引用,为什么我需要取消引用它?为什么我需要返回对象而不是对对象的引用,如返回类型所示?换句话说,为什么不这样做呢:
MyObject& MyObject::operator=(const &rhs)
{
// code to make this work
return this;
}
或者,为什么不这样做呢:
MyObject MyObject::operator=(const &rhs)
{
// code to make this work
return *this;
}
我很困惑。
【问题讨论】:
标签: c++ return-type assignment-operator lvalue