digitalmars.D - opCmp function
- Bruno A. Costa (9/9) May 03 2004 Hi all,
- J Anderson (8/19) May 03 2004 opCmp returns 3 states
- Ant (7/17) May 03 2004 0 means equal, <0 means one is lower, >0 means the other is lower.
- J Anderson (24/51) May 03 2004 class A
- Ant (8/59) May 03 2004 Sorry, my mistake, I was talking about:
- Bruno A. Costa (4/30) May 03 2004 I just wanted to learn D, so I started to write some easy wrappers to
Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit? And how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok. Thanks, Bruno.
May 03 2004
Bruno A. Costa wrote:Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit?opCmp returns 3 states 0: equal -1: less then 1: more thenAnd how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok.int opCmp (Byte bt) { return (_value - bt.value); }looks fine to me. -- -Anderson: http://badmama.com.au/~anderson/
May 03 2004
In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...--nextPart1697271.eHQoPkZ5kg Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit?0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)And how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok.I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte) So, are you doing an all OO lib? or just what you need? Ant
May 03 2004
Ant wrote:In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...class A { byte val; int opCmp(A _a) { return val - _a.val; } } int main ( char [] [] args ) { A a = new A; A b = new A; a.val = 10; b.val = 11; printf("%d < %d = %d\n", a.val, b.val, b < a); } This works. What are you talking about? You do need to do such things if you want to use sort with arrays (which I think is a bug). Other then that it should work fine.--nextPart1697271.eHQoPkZ5kg Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit?0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)And how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok.I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte)So, are you doing an all OO lib? or just what you need? Ant-- -Anderson: http://badmama.com.au/~anderson/
May 03 2004
In article <c766ek$32u$1 digitaldaemon.com>, J Anderson says...Ant wrote:Sorry, my mistake, I was talking about: http://www.digitalmars.com/d/phobos.html#object int cmp(Object obj) Compare with another Object obj. Returns: <0 for (this < obj) =0 for (this == obj)In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...class A { byte val; int opCmp(A _a) { return val - _a.val; } } int main ( char [] [] args ) { A a = new A; A b = new A; a.val = 10; b.val = 11; printf("%d < %d = %d\n", a.val, b.val, b < a); } This works. What are you talking about?--nextPart1697271.eHQoPkZ5kg Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit?0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)And how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok.I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte)0 for (this > obj)Ant
May 03 2004
Ant wrote:In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...I just wanted to learn D, so I started to write some easy wrappers to primitive types. But an OO lib would be a very nice thing to do. Bruno.--nextPart1697271.eHQoPkZ5kg Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Hi all, I was playing with D operator overloading and I found the opCmp function a bit strange. Why the type returned is an int? It would not have to return a bit?0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)And how should I code an opCmp overload? I tried the some coding (atached) and it worked well, but I am not sure if the opCmp overloading (at the end) is Ok.I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte) So, are you doing an all OO lib? or just what you need? Ant
May 03 2004