I’m building a class to handle complex numbers. I’ve created code for Operator_Add, Operator_Subtract, Operator_Multiply and Operator_Divide. These are working fine. Now, I would like to be able to include a numeric literal in the math expression, and have it automatically convert to a type Complex. For example, if a, b and c have been defined as complex, then I’d like to be able to do this:
c=2.5*(a+b)
where the 2.5 literal is converted to a type complex with the real part equal to 2.5 and the imaginary part equal to zero.
I thought that was the purpose of Operator_Convert, but when I define it for an argument of type double, it doesn’t seem to do anything, the math expression gives a syntax error saying “Expected class Complex, but got double.”
I did eventually implement overloaded versions of all of the operators, and the corresponding right operators, with type Double argument, and that did work.
However, I’m still left wondering, if I had to implement all of those overloaded versions, then what is the purpose of Operator_Convert?
Var Instance As New MyClass
Var Value As Double = Instance
In this case Operator_Convert() As Double would be called. The reverse is Operator_Convert(Value As Double) that acts as a constructor. To be clear, if fires instead of your constructor. It allows for code like
Okay thanks. I was hoping the compiler would be smart enough to recognize that an operand was different from the final assignment type, and automatically call operand_convert rather than having to have every operator overloaded with every possible operand type. Arrrrgh!
[quote=481038:@Markus Rauch]it sees only
Double*Complex
why it should convert double to something else?[/quote]
the language should convert these to a common type to do the comparison
ie/
dim i as integer
dim d as double
if i <> d then // <<< I and/orD are converted to a common type to do the comparison
end if
but there are cases where it doest do this even if operator_convert is defined
“When is operator convert called”
My answer was “not when you expect it might be or should be”
And the bug report gives one case where you might expect that it could/should be
But it isnt
The OP’s question is very similar
And the answer is “the order the expression is written in may be relevant” - unfortunately
IF you define operator_add then you should also define operator_addright
IF you define operator_multiply then you should also define operator_multiplyright
not doing so can lead to odd compiler bug
but it still doesnt solve the bug report I wrote
in that case the ONLY answer is to actually change the order of the comparison
Okay thanks. I guess I won’t make any more assumptions.
I’ve already discovered that I need to include all of the AddRight, SubtractRight, MultiplyRight etc., methods if I want it to work with doubles. I guess I could have taken the lazy route and just forced the user to convert everything to complex before using it in an expression, but that can get cumbersome.
The Operator_Compare is an interesting case, because other than comparing for equality, I don’t think there is any generally accepted way to determine if one complex number is greater than another one. So I probably don’t need to include this anyway.