Reputation: 1
What is the difference between returning *this
or the given argument in implementation of operator= in C++? Is using one of them better or more useful? if yes, why?
class Object {
public:
Object operator=(Object Obj) {
return *this;
}
}
vs.
class Object {
public:
Object operator=(Object Obj) {
return Obj;
}
}
Upvotes: 0
Views: 68
Reputation: 275896
X& operator=( X const& ) { return *this; }
matches the semantics of =
on an int
. The other suggestions you gave do not. When in doubt match the semantics of int
.
Upvotes: 1