type cl = record r,g,b:byte; end; var cl1,cl2:cl; begin if cl1<>cl2 then ... //operator not applicable to this operand type 

    7 answers 7

    Maybe so compare record variables ...

     if (cl1.r=cl2.r) and (cl1.g=cl2.g) and (cl1.b=cl2.b) then ... else ... {пиши код свой, если переменные не идентичны} 

      Using the definition of comparison operators. RTFM Operator Overloading .

      See example just with color Delphi operator overloading

      Threat Available in Delphi older than 7th version.

      ZZY Here is a list of innovations compared to D7.

      • one
        However, there are also minuses - the heavy and buggy environment, the documentation processed for the worse. - karmadro4
       type cl = record r,g,b:byte; class operator Equal(a, b: cl): Boolean; end; { ... } class operator cl.Equal(a, b: cl): Boolean; begin Result := (ar = br) and (ag = bg) and (ab = bb); end; 

      (terrible style of the code - the author's copy-paste)

      • one
        @Asen, this is operator overload. - Nofate
      • 3
        I got it. You still live in Delphi 7. Or just not up to date. In any case, bother to go and read the original sources . - Nofate
      • one
        In this particular case, @ karmadro4 overloaded the operator = . - Nofate
      • one
        @Nofate, better such a link . Unfortunately, Asen again seeks to reduce the discussion to a simple denial of what was said by the opponent, I think that it can be ignored. - karmadro4
      • 2
        @Nofate, I agree, it turned out quite inconspicuously (as in many other innovations in the Delphi dialect), but I disagree about borrowing keywords from the C family languages. In general, static is a modifier that has a different value . By the way, check out how the fpc architects approached the issue of operator overloading. - karmadro4

      The @integralal approach — field comparison — is correct. If you want to avoid this, you can compare the byte arrays with the CompareMem function, pre-declaring entries as packed.

       var p1, p2 : PSomeRecord; begin New(p1); New(p2); try if CompareMem(p1, p2, Sizeof(p1^)) then {...}; finally Dispose(p1); Dispose(p2); end; end; 

      For the method to work, the records must be packed tightly and do not contain pointers. In general, the method is unsafe.

      • 2
        And why can not compare records with pointers? - karmadro4
      • Because pointers can point to different parts of the memory, and the contents in them can be the same. CompareMem will compare only the pointers themselves. - kot-da-vinci

      Comparison of such variables as a rule depends on the business logic, only with full equality of all the fields it can be argued that they are equal, otherwise some business logic must be followed. Please provide the business logic of your particular case. If, as other participants have guessed, this is a comparison of two colors, then the easiest way is probably to do the procedure.

       function compareRGB(color1,color2:cl):boolean; begin if color1.r=color2.r and color1.g=color2.g and color1.b=color2.b then result := true else result := false; end; 
      • Funny: "Business Logic"! =))))) just a strategy. That you correctly noticed ... - AseN
      • one
        IMHO the author of the question RGB (color). I do not know how to do business, but in the sense of a person feeling the brightness of something like 30 * R + 50 * G + 20 * B. Then you can return 0 if equal to, -1 color1 <color2 and 1 if color1> color2 (or even the difference in brightness estimates). - avp
      • Somewhat strange syntax. And most importantly - boolshit, the conditional statement is not needed here at all. - karmadro4
       function IsEqual(a,b:C1): Boolean; begin Result:=(ar=br) and (ag=bg) and (ab=bb); end; 

        In Delphi 2006 and above, there is operator overloading for records:

         TKMPoint = record X,Y: Integer; public class operator Equal(const A, B: TKMPoint): Boolean; class operator NotEqual(const A, B: TKMPoint): Boolean; end; ......... class operator TKMPoint.Equal(const A, B: TKMPoint): Boolean; begin Result := (AX = BX) and (AY = BY); end; class operator TKMPoint.NotEqual(const A, B: TKMPoint): Boolean; begin Result := (AX <> BX) or (AY <> BY); end; ........ var A, B: TKMPoint; begin A := TKMPoint.New(1, 1); B := TKMPoint.New(1, 1); Assert(A = B); B := TKMPoint.New(2, 2); Assert(A <> B); end; 

        List of operators to override: Operator Overloading (Delphi)