I was working on some code recently and decided to work on my operator overloading in C ++, Because I have never really implemented it before. Therefore I have overload comparison operators using the comparison function for my matrix class, which returns 0, if the LHS was lower than the RHS, 1 if the LHS was larger than the RHS and 2 if it was the same. Then I exploited all the logical properties in C ++ on the integer, to compare all of its line in one line:
Inline Bool Matrix :: Operator & lt; (Matrix and RHS) {Return! (Comparison (* this, RHS)); } Inline Bool Matrix :: Operator> (Matrix and RHS) {Return! (Comparison ((* this), RHS) -1); } Inline Bull Matrix :: Operator & gt; = (Matrix and RHS) {Compare Return (* *), RHS); } Inline Baul Matrix :: Operator & lt; = (Matrix and RHS) {Compare Return (* *), RHS) -1; } Inline Bool Matrix :: Operator! = (Matrix and RHS) {Compare Return (* *), RHS) -2; } Inline Bool Matrix :: Operator == (Matrix and RHS) {Return! (Compare (* this), RHS) -2); }
Obviously I should pass the RHS as a constant, I am not using this matrix class again and I do not like to write any such function One was not the reference to achieve the only index value for the comparator operation.
According to the suggestion here is the code if the return is less than -1, equal to 0 and positive for 1
Inline Bool Matrix :: Operator & lt; (Matrix & amp;; RHS) {Return! (Compare (* this, RHS) +1); } Inline Bool Matrix :: Operator> (Matrix and RHS) {Return! (Comparison ((* this), RHS) -1); } Inline Bull Matrix :: Operator & gt; = (Matrix and RHS) {Compare Return (* This), RHS) +1; } Inline Baul Matrix :: Operator & lt; = (Matrix and RHS) {Compare Return (* *), RHS) -1; } Inline Bool Matrix :: Operator! = (Matrix and RHS) {Compare Return (* *), RHS); } Inline Bool Matrix :: Operator == (Matrix and RHS) {Return! (Compare (* this), RHS)); }
I do not know that this actually enhances readability.
As far as I can see it is safe, but every person reading this code Looks for twice. Why would you want to do this?
Anyway, for comparison, you should never call The comparison may be that you are not as smart, but whatever & lt;
and ==
or ! =
, the rest is needed is canonical and I write it mostly by muscle memory In addition to this, binary operators treated their operations equally (they leave them alone) IMO as non-members Should be implemented. Given this, by using the Comparative Comparison Function ( -1
, 0
, +1
) and the required const , I come to:
// The actual work is doing the Inline Bull Operator; (Const Metrix & amp; l, Const Metrix & amp; R) {Return -1 == Compare (L, R); } Inline Bull Operator == (Const Metrics and Lenses, Const Metrics and R) {Returns 0 == Comparison (L, R); } // Canonical Inline Bull Operator & gt; (Cost Matrix and Le, Const Metrics and R) {Return R & LT; L;} Inline Bull Operator & gt; = (Const Metrics & amp; l, Const Metrics and R) {Return! (L & T; R);} Inline Bull Operator & lt; = (Const Metrics & L, Const Metrics and R) {Return! (R & lt; L);} Inline Bull Operator! = (Const Metrix & L, Const Metrix and R) {Return! (L = RR);}
strcmp ()
has ever been seen, Whatever he does, he knows immediately. Note that I also have 0! = Comparison (...)
added, which is completely unnecessary - for the compiler The man makes it clear to the IMO that what is happening compared to the underlying artists on bool
, it is the same as the operator and lieutenant; Emphasizes symmetry for the implementation of
.
Comments
Post a Comment