Reputation: 39871
Operator= in C++ inside a class is being declared like this:
MyType & operator=(const MyType & rhs);
It is reasoned like it is necessary for chaining. But, as operator= has right precedence, then returning the value should be enough.
Upvotes: 1
Views: 230
Reputation: 7748
I find it's best to let the compiler to automatically generate operator=(). In the cases where you do actually need to specify it (for a deep copy most likely), I wouldn't do anything non-standard to it. This will only confuse other developers.
Don't be too clever :)
Upvotes: 0
Reputation: 10969
You can return by value, there is nothing to stop you. The "chaining" people refer to is statements like (a = b) = c
, which has the effect of assigning b
to a
, and then assigning c
to a
. This has almost no practical use, so if you chose to return by value that's perfectly fine.
The current state of affairs comes from the fact that for primitives, assignment is defined that way. So the compiler provided assignment operator works the same way, and generally you want overloaded operators to behave like their built in counterparts whenever possible. In this particular case, though, given the relative obscurity of that particular construct, you are unlikely to confuse anybody by changing that behavior. So long as you don't do something completely unexpected, like returning a boolean to indicate whether or not the assignment succeeded, it shouldn't matter.
Upvotes: 0
Reputation: 75130
If you don't return by reference, you are returning by value, and you can't assign to a value, because only lvalues are assignable. Even if you could, it wouldn't matter because the object you assigned to would be destroyed soon after you assigned to it because it's only a copy of the object, not the object itself.
In effect, you are trying to do this:
int blah() { int blah = 5; return blah; }
blah() = 99;
Which as you can see is obviously wrong.
It does depend on the order in which you do assignments though, because this issue will only come up when you change the natural order of assignments by making one on the left happen before one on the right, like Oli's example in a comment on this answer:
(a = b) = c
Another reason is to eliminate unnecessary copying, though compiler optimisations might take away that benefit.
You can read more on lvalues and rvalues here: http://msdn.microsoft.com/en-us/library/bkbs2cds.aspx
Upvotes: 0
Reputation:
You certainly can declare operator =
with a non-reference return type. In fact, on the very rare occasions I implement it, I normally make it return void
as I don't think that multiple assignments, or testing the result of assignment, are one of C++'s greatest features.
Upvotes: 4
Reputation: 12212
Yes, but the reason may or may not have nothing to do with precedence. The reason to return a reference, and not a value, is the same one for passing rhs (in your example) as a constant reference instead of a value: better performance. So you can return only the value if you want, but take into account that a copy may be created.
Also you have to take into account whether your class is prepared or not for copies.
Upvotes: 0