Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 11, 2005 does array.sort work at all for opCmp? | ||||
---|---|---|---|---|
| ||||
dmd 129: sortbug.exe 1.000000 3.000000 2.000000 2.000000 3.000000 1.000000 sortbug.d ------------------------------------------ class A { public: float value; this(float v) {value=v;} int opCmp(A o) { if (this.value < o.value) return -1; if (this.value > o.value) return 1; return 0; } } int main(char[][] args) { int i; A[3] arr; arr[0] = new A(1.0); arr[1] = new A(3.0); arr[2] = new A(2.0); for (i=0; i<3; i++) { printf("%f ", arr[i].value); } printf("\n"); arr.sort; for (i=0; i<3; i++) { printf("%f ", arr[i].value); } printf("\n"); return 0; } ------------------------------------------ |
August 11, 2005 Re: does array.sort work at all for opCmp? | ||||
---|---|---|---|---|
| ||||
Posted in reply to z | You need to define int opCmp(Object o){ ... }
z@gg.com wrote:
> dmd 129:
>
> sortbug.exe
> 1.000000 3.000000 2.000000
> 2.000000 3.000000 1.000000
>
>
> sortbug.d
> ------------------------------------------
> class A {
> public:
> float value;
>
> this(float v) {value=v;}
>
> int opCmp(A o) {
> if (this.value < o.value) return -1;
> if (this.value > o.value) return 1;
> return 0;
> }
>
> }
> int main(char[][] args)
> {
> int i;
> A[3] arr;
>
> arr[0] = new A(1.0);
> arr[1] = new A(3.0);
> arr[2] = new A(2.0);
> for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
> printf("\n");
> arr.sort;
> for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
> printf("\n");
>
> return 0;
> }
> ------------------------------------------
>
>
|
August 11, 2005 Re: does array.sort work at all for opCmp? | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | zwang wrote: > You need to define int opCmp(Object o){ ... } ...and I think it should be a compiler warning trying to define only opCmp(MyObj) without defining opCmp(Object) for objects. It would save us from so much hassle. The same thing for not defining toHash when defininng opEquals and so forth. -- Niko Korhonen SW Developer |
August 11, 2005 Re: does array.sort work at all for opCmp? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niko Korhonen | >zwang wrote:
>> You need to define int opCmp(Object o){ ... }
Does D support covariant argument type for class method?
|
Copyright © 1999-2021 by the D Language Foundation