sort!("a < b")( arr ); - opCmp
David Nadlinger
see at klickverbot.at
Fri Jun 17 17:17:39 PDT 2011
On 6/18/11 1:40 AM, Joel Christensen wrote:
> How do you override "int opCmp( Object obj )"? What can I do with obj?
You can do pretty much whatever you want with it – obviously, you should
not modify obj, because comparison is not generally expected to modify
its arguments, but at the moment, that's not enforced via const or
something like that.
That said, your opCmp should return a negative number (-1 usually) if a
is smaller than b, 0 if they are equal, and a positive number (1) if a
is larger than b.
An implementation of Square.opCmp might look like this (untested):
---
override int opCmp(Object o) {
auto rhs = cast(Square)o;
if (rhs) {
return (<whatever you want, using this and rhs members>);
}
// o wasn't actually a Square, pass it to the base class
// implementation (the one in Object errors out).
return super.opCmp(o);
}
---
David
More information about the Digitalmars-d-learn
mailing list