|
-
Sep 5th, 2022, 02:59 PM
#1
[RESOLVED] Why No = Operator for ISomething and ISomething
I have a few classes where I overloaded the = operator for convenience sake. Some of this was done many years ago, before I recognized the great value of putting an interface on...pretty much everything. Therefore, when I went back and added interfaces to some of these classes, I had to change the implementations of the Operator = (and <>, of course, since you can't have one without the other).
These methods all take two operands, which will be the left and right operand. For class C, which implements IC, if I want to use IC everywhere, I end up creating two sets of these operators:
Operator = (A as C, B as IC)
and
Operator = (A as IC, B as C)
since I don't know in which order they will be passed. However, what will most often happen is that both A and B would make most sense as IC, and that won't work, because I am not allowed to implement:
Operator = (A as IC, B as IC)
even if the body of such an operator would be trivial (it isn't always).
The reason given for not allowing this, if I am understanding the message right, is that for class C, if there is operator overloading, at least one of the operands must be type C. What is the purpose for this? It's somewhat awkward, in my case, though easy enough to get around, since Equals also has to be implemented, and that can be used in place of IC = IC, but what is the reason why that third operator overloading is not allowed?
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|