Beginner problem: casting in opCmp override
Jonathan M Davis
jmdavisProg at gmx.com
Mon Jul 8 16:25:54 PDT 2013
On Tuesday, July 09, 2013 00:35:32 Ugbar Ikenaki wrote:
> Here's one more question:
>
> Before I knew that opEquals existed, I tried overloading the
> equality expressions (== and !=) in opBinary using the code
> below. It worked. Why would the overloaded opBinary version get
> called if the equality expressions are held in opEquals? I'm just
> interested in knowing how dmd chooses where to take an expression
> from if it lies in multiple places (e.g. == in opEquals and
> opBinary), even if what I did was just add the == operator to
> opBinary. (Is that what I did there?)
>
> override bool opBinary( string op ) ( Rect r ) if( op ==
> "==" ) {
> //Check if rectangle coordinates are equal
> if( left == r.left && right == r.right && top == r.top &&
> bottom == r.bottom ) {
> return true;
> } else {
> return false;
> }
> }
>
>
> Thanks!
I don't believe that that will ever work. If you want to overload == and !=,
use opEquals. opBinary isn't used for that. Look at
http://dlang.org/operatoroverloading.html if you want to see which function to
use to overload for each operator.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list