【发布时间】:2020-08-26 02:37:14
【问题描述】:
在 C++ 中实现 operator= 时返回 *this 或给定参数有什么区别?使用其中之一更好或更有用吗?如果是,为什么?
class Object {
public:
Object operator=(Object Obj) {
return *this;
}
}
对比
class Object {
public:
Object operator=(Object Obj) {
return Obj;
}
}
【问题讨论】:
-
您应该返回对当前对象的引用,而不是一个全新的对象。
-
这能回答你的问题吗? What is The Rule of Three?
标签: c++ operator-overloading this operator-keyword