A Fresh Look at Comparisons, Take 2

Jason House jason.james.house at gmail.com
Fri Apr 18 08:28:51 PDT 2008


Janice Caron Wrote:

> On 18/04/2008, Jason House <jason.james.house at gmail.com> wrote:
> > IMHO, explicit is dangerous.  It suffers from all the normal pitfalls of non-virtual function calls.  Explicit helps when you have all the raw objects and have to be explicit in how you use them, but when calling functions that accept a base class or using an object factory, it gets more complex.
> 
> You wouldn't use it in that circumstance. You'd use it when you needed
> it - i.e. when you desired the behavior it provides.
> 
> It neatly solves the problem of
> 
>     class A
>     {
>         /* provide opCmp */
>     }
>     class B : A {}
>     class C : A {}
> 
>     B b = new B;
>     C c = new C;
>     if (b < c) ...
> 
> doing the wrong thing.

How about this:
class A
{
  /* provide opCmp */
}
bool foo(A a1, A a2){ return a1<a2; }
...
class B : A {}
class C : A {}
B b = new B;
C c = new C;
return foo(B,C);


I'm sure you could say that foo must now declare its parameters as explicit, but requiring all functions that compare A's to have explicit parameters really starts to kill the whole utility of having a base class to begin with.



More information about the Digitalmars-d mailing list